1. 程式人生 > 實用技巧 >Android MediaStore操作檔案記錄

Android MediaStore操作檔案記錄

google官方文件

github地址

oppo給出的適配指南

Android 10適配要點,作用域儲存


儲存的uri路徑content://media/external/images/media/498662

建立圖片

 1 private void createFile() {
 2 
 3         Uri contentUri = MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL);
 4         ContentValues contentValues = new ContentValues();
5 long dateTaken = System.currentTimeMillis(); 6 contentValues.put(MediaStore.Images.Media.DATE_TAKEN, dateTaken); 7 contentValues.put(MediaStore.Images.Media.DESCRIPTION, "建立的第一張圖片"); 8 contentValues.put(MediaStore.Images.Media.IS_PRIVATE, 1); 9 contentValues.put(MediaStore.Images.Media.DISPLAY_NAME, "test");
11 contentValues.put(MediaStore.Images.Media.MIME_TYPE, "image/png"); 12 contentValues.put(MediaStore.Images.Media.TITLE, "圖片"); 14 contentValues.put(MediaStore.Images.Media.RELATIVE_PATH, "DCIM/test"); 15 long dateAdded = System.currentTimeMillis(); 16 contentValues.put(MediaStore.Images.Media.DATE_ADDED, dateAdded);
17 long dateModified = System.currentTimeMillis(); 18 contentValues.put(MediaStore.Images.Media.DATE_MODIFIED, dateModified); 19 20 Uri insert = getContentResolver().insert(contentUri, contentValues); 21 Log.d(TAG, "createFile: url : " + insert); 22 toast("createFile: url : " + insert); 23 try { 24 OutputStream outputStream = getContentResolver().openOutputStream(insert); 25 Bitmap bitmap = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888); 26 bitmap.compress(Bitmap.CompressFormat.PNG, 90, outputStream);33 outputStream.write(33); 34 } catch (Exception e) { 35 e.printStackTrace(); 36 }finally { 37 showImg(insert); 38 } 39 }

查詢對應name的圖片

 1 private Uri selectSingle() {
 2         Uri queryUri = null;
 3         Uri external = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
 4         String selection = MediaStore.Images.Media.DISPLAY_NAME+"=?";
 5         String[] args = new String[]{"test.png"};
 6         String[] projection = new String[]{MediaStore.Images.Media._ID};
 7         Cursor cursor = getContentResolver().query(external, projection, selection, args, null);
 8         if (cursor != null && cursor.moveToFirst()) {
 9             queryUri = ContentUris.withAppendedId(external, cursor.getLong(0));
10             Log.d(TAG, "selectSingle 查詢成功,Uri路徑 : "+queryUri);
11             toast(queryUri.toString());
12             showImg(queryUri);
13             cursor.close();
14         }else{
15             Log.d(TAG, "selectSingle 查詢失敗");
16         }
17         return queryUri;
18     }

更新檔案

 1 private void updateFile() {
 2         int SENDER_REQUEST_CODE = 3;
 3         if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
 4             toast("update : 沒有讀取許可權" );
 5             return;
 6         }
 7         if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
 8             toast("update : 沒有寫入許可權" );
 9             return;
10         }
11         Uri uri = selectSingle();
12         if (TextUtils.isEmpty(uri.toString())){
13             toast("update : uri為 null " );
14             return;
15         }
16         try {
17             OutputStream outputStream = getContentResolver().openOutputStream(uri);
18             Bitmap bitmap = Bitmap.createBitmap(500, 500, Bitmap.Config.ARGB_8888);
19             bitmap.compress(Bitmap.CompressFormat.PNG, 90, outputStream);
20             Canvas canvas = new Canvas(bitmap);
21             canvas.drawColor(Color.YELLOW);
22             Paint paint = new Paint();
23             paint.setColor(Color.BLACK);
24             paint.setTextSize(20);
25             canvas.drawText("修改",100,100,paint);
26             outputStream.write(33);
27             outputStream.close();
28         } catch (FileNotFoundException e) {
29             e.printStackTrace();
30         }catch (@SuppressLint("NewApi") RecoverableSecurityException e1) {
31             e1.printStackTrace();
32             //捕獲 RecoverableSecurityException異常,發起請求
33             try {
34                 startIntentSenderForResult(
35                         e1.getUserAction().getActionIntent().getIntentSender(),
36                         SENDER_REQUEST_CODE,
37                         null,
38                         0,
39                         0,
40                         0
41                 );
42             } catch (Exception e){
43                 e.printStackTrace();
44             }
45         } catch (IOException e) {
46             e.printStackTrace();
47         }
48     }

刪除檔案

 1 private void deleteFile() {
 2 
 3         System.out.println("MediaStore.Images.Media.DISPLAY_NAME = " + MediaStore.Images.Media.DISPLAY_NAME);
 4 
 5         Uri CONTENT_URI = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
 6         String selectionclause = MediaStore.Images.Media.DISPLAY_NAME+"=?";
 7         String[] arguments = new String[]{"test.png"};
 8         int delete = getContentResolver().delete(CONTENT_URI, selectionclause, arguments);
 9         Log.d(TAG, "deleteFile: " + delete);
10         if (delete > -1){
11     //        如果發生異常返回-1
12             toast("delete : " + delete);
13         }else{
14             Log.d(TAG, "deleteFile: 失敗 " + delete);
15         }
16     }