zoukankan      html  css  js  c++  java
  • 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     }
  • 相关阅读:
    select top 变量问题
    distinct top執行順序
    Subquery typo with using in(转)
    sql:查询课程号'0312091006'成绩排名第5到第10之间的学生学号
    case when then
    触发器
    索引
    管理事物处理
    053345
    053344
  • 原文地址:https://www.cnblogs.com/xjean/p/13679043.html
Copyright © 2011-2022 走看看