Android MediaStore操作文件記錄


google官方文檔  

github地址

oppo給出的適配指南

huwei給的是適配指南

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


 先申請權限

 1 private void grantPermission() {
 2 // 使用Environment.isExternalStorageLegacy()來檢查APP的運行模式
 3         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q &&
 4                 !Environment.isExternalStorageLegacy()) {
 5             System.out.println("btnCreate = isExternalStorageLegacy : " + Environment.isExternalStorageLegacy());
 6         }
 7         if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
 8             ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
 9                     REQUEST_PERMISSION);
10             Log.d(TAG, "grantPermission: 權限說明提示");
11         } else {
12             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
13                 Log.d(TAG, "grantPermission: requestPermissions");
14                 requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_PERMISSION);
15 
16             }
17         }
18 
19     }

 

儲存的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     }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM