• Android APP 存储权限(适用高于Android R版本)


    1. 低于Android R版本

    1.1 修改AndroidManifest.xml :

    
    
    
    android:requestLegacyExternalStorage="true"

     1.2 代码增加权限申请

    1. if (ContextCompat.checkSelfPermission(this,
    2. Manifest.permission.WRITE_EXTERNAL_STORAGE)
    3. != PackageManager.PERMISSION_GRANTED
    4. || ContextCompat.checkSelfPermission(this,
    5. Manifest.permission.READ_EXTERNAL_STORAGE)
    6. != PackageManager.PERMISSION_GRANTED) {
    7. //多个权限一起申请
    8. ActivityCompat.requestPermissions(this, new String[]{
    9. Manifest.permission.WRITE_EXTERNAL_STORAGE,
    10. Manifest.permission.READ_EXTERNAL_STORAGE,
    11. }, 1);
    12. }

    1. @Override
    2. public void onRequestPermissionsResult(int requestCode, String[] permission, int[] grantResult) {
    3. super.onRequestPermissionsResult(requestCode, permission, grantResult);
    4. switch (requestCode) {
    5. case 1:
    6. if (grantResult.length>0){
    7. boolean readper = grantResult[0] == PackageManager.PERMISSION_GRANTED;
    8. if(readper){
    9. Toast.makeText(getApplicationContext(), "Permission Granted", Toast.LENGTH_SHORT).show();
    10. }
    11. else {
    12. Toast.makeText(getApplicationContext(), "Permission Denied", Toast.LENGTH_SHORT).show();
    13. }
    14. } else {
    15. Toast.makeText(getApplicationContext(), "You Denied Permission", Toast.LENGTH_SHORT).show();
    16. }
    17. break;
    18. default:
    19. break;
    20. }
    21. }

    2. 高于Android R版本

    2.1 方法1: 申请MANAGE_EXTERNAL_STORAGE权限

    修改AndroidManifest.xml :
    
    1. public void askForPermissions() {
    2. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
    3. if (!Environment.isExternalStorageManager()) {
    4. Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
    5. intent.setData(Uri.parse("package:" + getPackageName()));
    6. startActivityForResult(intent, REQ_PERMISSION_CODE);
    7. }
    8. }
    9. }

    2.2 方法2:通过InputStream/OutputStream读写文件

    1. //通过FileOutputStream 写文件
    2. public static FileOutputStream GetFileOutputStreamFromFile(String fileUrl, Context context){
    3. FileOutputStream Fout = null;
    4. OutputStream out = null;
    5. //获取文件名
    6. String FileName = fileUrl.substring(fileUrl.lastIndexOf("/") + 1).toLowerCase();
    7. Log.i(TAG,"GetFileOutputStreamFromFile2->FileName:"+ FileName);
    8. final ContentValues values = new ContentValues();
    9. values.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_MUSIC + File.separator + "AudioTest"); //路径
    10. values.put(MediaStore.MediaColumns.DISPLAY_NAME, FileName);//文件名
    11. values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/basic");
    12. values.put(MediaStore.MediaColumns.IS_PENDING, 1);
    13. ContentResolver resolver = context.getContentResolver();
    14. final Uri uri=resolver.insert(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, values);
    15. //uri转换为string路径
    16. Log.i(TAG,"GetFileOutputStreamFromFile2->uri:"+ uri);
    17. String[] proj = { MediaStore.Audio.Media.DATA };
    18. Cursor cursor = resolver.query(uri,proj,null,null,null);
    19. int column_index = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
    20. cursor.moveToFirst();
    21. String file_path = cursor.getString(column_index);
    22. Log.i(TAG,"GetFileOutputStreamFromFile2->path:"+ file_path);
    23. PCMFilePath = file_path;
    24. //openOutputStream
    25. try {
    26. out = resolver.openOutputStream(uri);
    27. }catch (IOException e) {
    28. e.printStackTrace();
    29. }
    30. //OutputStream转FileOutputStream
    31. if (out instanceof FileOutputStream) {
    32. Fout = (FileOutputStream) out;
    33. }else{
    34. Log.e(TAG,"converto FileOutputStream failed.");
    35. }
    36. return Fout;
    37. }
    38. //读文件
    39. public static FileInputStream GetFileInputStreamFromFile(String fileUrl)
    40. FileInputStream Fin = null;
    41. File file_in = new File(fileUrl);
    42. try {
    43. Fin = new FileInputStream(file_in);
    44. }catch (IOException e) {
    45. e.printStackTrace();
    46. return;
    47. }
    48. return Fin;
    49. }

  • 相关阅读:
    华为CSS堆叠技术介绍与实现
    Linux 系统误将 chmod 权限改成 了 000,如何恢复?
    wu-framework-parent 项目明细
    Nginx反向代理配置
    mcu日志输出的一种方法
    距离Java大神还有多远
    python基于django或flask开发的健身俱乐部网站rix1z
    CSS动画实现节流
    如何在Mac电脑上安装WeasyPrint:简单易懂的步骤
    如何在单链表中的任意一个位置插入一个结点
  • 原文地址:https://blog.csdn.net/liaochaoyun/article/details/126128896