在广播接收器上,检查写入权限 android M

On broadcast receiver, check for write permission android M

我使用媒体文件夹 android.hardware.action.NEW_PICTURE 服务的广播接收器使用此代码重命名和移动图像并且它正在运行:

CameraReciver
包 perim.ebrahimi.ir.perim;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;

public class CameraReciver extends BroadcastReceiver {

    private States states;
    private SessionManager session;
    private WriteService main;

    @Override
    public void onReceive(Context context, Intent intent) {
        getStates(context);
        if(states.getAPP()){

            Cursor cursor = context.getContentResolver().query(intent.getData(), null, null, null, null);
            cursor.moveToFirst();
            if(cursor!=null){
                String image_path = cursor.getString(cursor.getColumnIndex("_data"));
                main = new WriteService();
                main.writeFolder(image_path, states, context);
            }
            cursor.close();
        } else abortBroadcast();
    }

    // OK
    private void getStates(Context context){
        session = new SessionManager(context.getApplicationContext());
        states = new States();
        states = session.getSession();
    }

}

这里是 WriteService:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package perim.ebrahimi.ir.perim;

import android.app.Activity;
import android.app.Application;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.widget.Toast;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class WriteService extends Activity {

    private States states;
    private Context context;
    private static CalendarJalalian cal;
    private static int day   ;
    private static int month ;
    private static int year  ;
    private static int saat  ;
    private static int minut ;
    private static int secnd ;
    private int orientation = -1;
    private static final int REQUEST_EXTERNAL_STORAGE = 100;
    private static final int REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS=200;
    private boolean mExternalStorageAvailable = false;
    private boolean mExternalStorageWriteable = false;

//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        //setContentView(R.layout.activity_main);
//
//        // A simple check of whether runtime permissions need to be managed
//        if (Build.VERSION.SDK_INT >= 23) {
//            checkMultiplePermissions();
//        }
//    }

    public void writeFolder(String image_path, States _states, Context _context){
        states = _states;
        context=  _context;
        cal = new CalendarJalalian();
        long fileSize = new File(image_path).length();
        Cursor mediaCursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                new String[] {MediaStore.Images.ImageColumns.ORIENTATION,
                        MediaStore.MediaColumns.SIZE },
                MediaStore.MediaColumns.DATE_ADDED +">=?",
                new String[]{String.valueOf(cal.getTimeInMillis()/1000 - 1)},
                MediaStore.MediaColumns.DATE_ADDED +" desc");
        if (mediaCursor != null && mediaCursor.getCount() !=0 ) {
            while(mediaCursor.moveToNext()){
                long size = mediaCursor.getLong(1);
                if(size == fileSize){
                    orientation = mediaCursor.getInt(0);
                    break;
                }
            }
        }
        orientation = (orientation<0)?0:orientation;
        image_path = changeName(image_path);
        if(image_path.length()==0) return;
        String image_jadid = copyFile(image_path);
        if(states.getMain() && image_jadid.length()>0){
            removeMain(image_path);
            removeMedia(context, new File(image_path));
        }
        if(states.getPayam()) Toast.makeText(this, getText(R.string.imageSaved)+":"+states.getKhas(), 500).show();

    }

    private void checkExternalStorage(){
        String[] aaa = new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE};
        ActivityCompat.requestPermissions(this, aaa , REQUEST_EXTERNAL_STORAGE);
        if(ContextCompat.checkSelfPermission(context, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_EXTERNAL_STORAGE);
        } else {
            String state = Environment.getExternalStorageState();
            if (Environment.MEDIA_MOUNTED.equals(state)) {
                mExternalStorageAvailable = mExternalStorageWriteable = true;
            } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
                mExternalStorageAvailable = true;
                mExternalStorageWriteable = false;
            } else {
                mExternalStorageAvailable = mExternalStorageWriteable = false;
            }
        }
    }

    private static void removeMedia(Context context, File f) {
        ContentResolver resolver = context.getContentResolver();
        resolver.delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, MediaStore.Images.Media.DATA +"=?", new String[] { f.getAbsolutePath() });
    }

    // OK
    private String changeName(String image_path){
        String result ="";
        day   = cal.getDay();
        month = cal.getMonth();
        year  = cal.getYear();
        saat  = Integer.parseInt(cal.getHHour());
        minut = Integer.parseInt(cal.getMinute());
        secnd = Integer.parseInt(cal.getSecond());

        String persianDateName = String.format("%1$s_%2$s_%3$s__%4$s_%5$s_%6$s.jpg",
                year,
                ((month<10)?"0"+month:month),
                ((day<10)?"0"+day:day),
                ((saat<10)?"0"+saat:saat),
                ((minut<10)?"0"+minut:minut),
                ((secnd<10)?"0"+secnd:secnd));

        File from = new File(image_path);
        if(from.exists()){
            if(states.getOnimage()){
                addTextToImage(image_path, persianDateName);
            }
            File to = new File(from.getParentFile(),persianDateName);
            try
            {
                boolean b = from.renameTo(to);
                if (!b) {
                    copy(from, to);
                    from.delete();
                }
                removeMedia(context, from);
                sendToMedia(to, persianDateName);
                result = to.getAbsolutePath();
            }
            catch(Exception ex){
                ex.printStackTrace();
            }
        }
        return result;
    }

    private void addTextToImage(String from, String persianDateName) {
        Log.i("info","Draw"+persianDateName+" on image");
        try
        {
            Log.i("info","1");
            FileOutputStream fos = new FileOutputStream(from);
            if(fos==null) Log.i("info","fos = null");

            Log.i("info","2 ="+from);
            Bitmap bitmap = BitmapFactory.decodeFile(from);
            if(bitmap==null) Log.i("info","bitmap = null");

//          Log.i("info","3");
            android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig();
            if(bitmapConfig==null) Log.i("info","bitmapConfig = null");

//          Log.i("info","4");
//          if (bitmapConfig == null) {
//              bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
//          }

            Log.i("info","5");
            bitmap = bitmap.copy(bitmapConfig, true);

            Log.i("info","6");
            Canvas canvas = new Canvas(bitmap);

            Log.i("info","7");
            Resources resources = this.getResources();
            float scale = resources.getDisplayMetrics().density;

            Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
            paint.setColor(Color.rgb(61, 61, 61));
            paint.setTextSize((int) (14 * scale));
            paint.setShadowLayer(1f, 0f, 1f, Color.WHITE);
            Rect qab = new Rect();
            paint.getTextBounds(persianDateName, 0, persianDateName.length(), qab);
            int x = (bitmap.getWidth() - qab.width()) / 2;
            int y = (bitmap.getHeight() + qab.height()) / 2;
            canvas.drawText(persianDateName, x, y, paint);

            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        Log.i("info","Text added on image");
    }

    private void copy(final File f1, final File f2) throws IOException {
        if(f2.exists()) f2.delete();
        //checkExternalStorage();
        checkMultiplePermissions();
        if(mExternalStorageAvailable && mExternalStorageWriteable) {
            f2.createNewFile();
            final RandomAccessFile file1 = new RandomAccessFile(f1,"r");
            final RandomAccessFile file2 = new RandomAccessFile(f2,"rw");
            file2.getChannel().write(file1.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, f1.length()));
            file1.close();
            file2.close();
        }
    }

    private boolean canRename(final File f1, final File f2) {
        final String p1 = f1.getAbsolutePath().replaceAll("^(/mnt/|/)","");
        final String p2 = f2.getAbsolutePath().replaceAll("^(/mnt/|/)","");
        return p1.replaceAll("\\\\/\\\\w+","").equals(p2.replaceAll("\\\\/\\\\w+",""));
    }

    // OK
    private void removeMain(String image_path){
        File f = new File(image_path);
        if(f.exists()) f.delete();
    }


    // OK
    private File createFileName(){
        day   = cal.getDay();
        month = cal.getMonth();
        year  = cal.getYear();
        saat  = Integer.parseInt(cal.getHHour());
        minut = Integer.parseInt(cal.getMinute());
        secnd = Integer.parseInt(cal.getSecond());

        String persianDateName = String.format("%1$s_%2$s_%3$s__%4$s_%5$s_%6$s.jpg",
                year,
                ((month<10)?"0"+month:month),
                ((day<10)?"0"+day:day),
                ((saat<10)?"0"+saat:saat),
                ((minut<10)?"0"+minut:minut),
                ((secnd<10)?"0"+secnd:secnd));

        String masirFolder ="";

        if(states.getSal())   masirFolder += year;
        if(states.getMah())   masirFolder += File.separator+ year+"_"+((month<10)?"0"+month:month);
        if(states.getRuz())   masirFolder += File.separator+ year+"_"+((month<10)?"0"+month:month)+"_"+((day<10)?"0"+day:day);

        if(states.getSaat())  masirFolder += File.separator+ year+"_"+((month<10)?"0"+month:month)+"_"+((day<10)?"0"+day:day)+"_"+((saat<10)?"0"+saat:saat);
        if(states.getMinut()) masirFolder += File.separator+ year+"_"+((month<10)?"0"+month:month)+"_"+((day<10)?"0"+day:day)+"_"+((saat<10)?"0"+saat:saat)+"_"+((minut<10)?"0"+minut:minut);

        if(states.getKhas().length()>0) masirFolder += File.separator+states.getKhas();

        File directTime = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), masirFolder);

        if (!directTime.mkdir()) makeDir(directTime);

        directTime = new File(directTime, persianDateName);

        return directTime;

    }

    // OK
    private void makeDir(File direct){
        direct.mkdirs();
    }

    // OK
    private String copyFile(String image_path){
        String masir ="";
        File sourceFile = new File(image_path);
        if (!sourceFile.exists()) {return masir;}
        File destinationFile = createFileName();
        FileChannel source = null;
        FileChannel destination = null;

        try {
            source = new FileInputStream(sourceFile).getChannel();
            destination = new FileOutputStream(destinationFile).getChannel();
            if (destination != null && source != null) {
                destination.transferFrom(source, 0, source.size());
            }
            if (source != null) {
                source.close();
            }
            if (destination != null) {
                destination.close();
            }
            masir = destinationFile.getName();
            sendToMedia(destinationFile, masir);
            masir = destinationFile.getAbsolutePath();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return masir;
    }

    // OK
    private void sendToMedia(File imageFile, String imageTitle){
        ContentValues image = new ContentValues();
        Date dateTaken = new Date();

        File parent = imageFile.getParentFile();
        String path = parent.toString().toLowerCase();
        String name = parent.getName().toLowerCase();

        image.put(MediaStore.Images.Media.TITLE, imageTitle);
        image.put(MediaStore.Images.Media.DISPLAY_NAME, String.format(this.getText(R.string.imageDisplayName).toString(),states.getKhas()));
        image.put(MediaStore.Images.Media.DESCRIPTION, String.format(this.getText(R.string.imageDescription).toString(),name));
        image.put(MediaStore.Images.Media.DATE_ADDED, dateTaken.toString());
        image.put(MediaStore.Images.Media.DATE_TAKEN, dateTaken.toString());
        image.put(MediaStore.Images.Media.DATE_MODIFIED, dateTaken.toString());
        image.put(MediaStore.Images.Media.MIME_TYPE,"image/png");
        image.put(MediaStore.Images.Media.ORIENTATION, orientation);//getImageOrientation(imageFile.getAbsolutePath()));

        image.put(MediaStore.Images.ImageColumns.BUCKET_ID, path.hashCode());
        image.put(MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME, name);
        image.put(MediaStore.Images.Media.SIZE, imageFile.length());
        image.put(MediaStore.Images.Media.DATA, imageFile.getAbsolutePath());

        this.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, image);
    }

    private void checkMultiplePermissions() {

        if (Build.VERSION.SDK_INT >= 23) {
            List<String> permissionsNeeded = new ArrayList<String>();
            List<String> permissionsList = new ArrayList<String>();

            if (!addPermission(permissionsList, android.Manifest.permission.ACCESS_FINE_LOCATION)) {
                permissionsNeeded.add("GPS");
            }

            if (!addPermission(permissionsList, android.Manifest.permission.READ_EXTERNAL_STORAGE)) {
                permissionsNeeded.add("Read Storage");
            }

            if (permissionsList.size() > 0) {
                requestPermissions(permissionsList.toArray(new String[permissionsList.size()]),
                        REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
                return;
            }
        }
    }

    private boolean addPermission(List<String> permissionsList, String permission) {
        try {
            if (Build.VERSION.SDK_INT >= 23)

                if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
                    permissionsList.add(permission);

                    if (!shouldShowRequestPermissionRationale(permission))
                        return false;
                }
        } catch(Exception ex){
            ex.printStackTrace();
        }
        return true;
    }
}

但是类 addPermission 引发错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'int
android.content.Context.checkSelfPermission(java.lang.String)' on a
null object reference

我认为我调用活动的方式不正确,但我不知道该怎么做,请帮助。

编辑:
我在返回的路径中发现了这种差异:

1
String image_path = cursor.getString(cursor.getColumnIndex("_data"));

变化的部分很明显:

1
2
old but correct path: /storage/emulated/0/DCIM/Camera/IMG_20161215_173334.jpg
new but incorrect path: /storage/3466-033/DCIM/Camera/IMG_20161215_173334.jpg

我认为由于新设备允许用户选择保存图像的位置,因此返回地址会相应更改。
如何从光标中获取正确的地址?


广播接收器无法做到这一点。你可以通过ContextCompat.checkSelfPermission检查你是否有权限,但是为了请求权限,你需要调用ActivityCompat.requestPermissions。它需要一个活动,结果也会到达那个活动。

鉴于此,对您来说最好的解决方案是实施一些活动,向用户解释正在发生的事情并请求许可。如果广播接收器检测到它没有必要的权限,它将启动此活动。授予权限后,将恢复正常操作。

我不得不说,作为一个用户,如果突然弹出一个带有权限请求的对话框,我会觉得很奇怪,所以我认为最好有一个活动来请求权限.

但是,从您的路径来看,它肯定在 SD 卡上的某个位置。这意味着 SD 卡写入限制适用。这意味着请求写入权限将无济于事。看看这个问题:如何避免SD卡出现"EACCES权限被拒绝"?