android:requestLegacyExternalStorage="true"
- if (ContextCompat.checkSelfPermission(this,
- Manifest.permission.WRITE_EXTERNAL_STORAGE)
- != PackageManager.PERMISSION_GRANTED
- || ContextCompat.checkSelfPermission(this,
- Manifest.permission.READ_EXTERNAL_STORAGE)
- != PackageManager.PERMISSION_GRANTED) {
- //多个权限一起申请
- ActivityCompat.requestPermissions(this, new String[]{
- Manifest.permission.WRITE_EXTERNAL_STORAGE,
- Manifest.permission.READ_EXTERNAL_STORAGE,
- }, 1);
- }
- @Override
- public void onRequestPermissionsResult(int requestCode, String[] permission, int[] grantResult) {
- super.onRequestPermissionsResult(requestCode, permission, grantResult);
- switch (requestCode) {
- case 1:
- if (grantResult.length>0){
- boolean readper = grantResult[0] == PackageManager.PERMISSION_GRANTED;
- if(readper){
- Toast.makeText(getApplicationContext(), "Permission Granted", Toast.LENGTH_SHORT).show();
- }
- else {
- Toast.makeText(getApplicationContext(), "Permission Denied", Toast.LENGTH_SHORT).show();
- }
- } else {
- Toast.makeText(getApplicationContext(), "You Denied Permission", Toast.LENGTH_SHORT).show();
- }
- break;
- default:
- break;
- }
- }
修改AndroidManifest.xml :
- public void askForPermissions() {
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
- if (!Environment.isExternalStorageManager()) {
- Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
- intent.setData(Uri.parse("package:" + getPackageName()));
- startActivityForResult(intent, REQ_PERMISSION_CODE);
- }
- }
- }
2.2 方法2:通过InputStream/OutputStream读写文件
- //通过FileOutputStream 写文件
- public static FileOutputStream GetFileOutputStreamFromFile(String fileUrl, Context context){
- FileOutputStream Fout = null;
- OutputStream out = null;
- //获取文件名
- String FileName = fileUrl.substring(fileUrl.lastIndexOf("/") + 1).toLowerCase();
- Log.i(TAG,"GetFileOutputStreamFromFile2->FileName:"+ FileName);
-
- final ContentValues values = new ContentValues();
- values.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_MUSIC + File.separator + "AudioTest"); //路径
- values.put(MediaStore.MediaColumns.DISPLAY_NAME, FileName);//文件名
- values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/basic");
- values.put(MediaStore.MediaColumns.IS_PENDING, 1);
-
- ContentResolver resolver = context.getContentResolver();
- final Uri uri=resolver.insert(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, values);
-
- //uri转换为string路径
- Log.i(TAG,"GetFileOutputStreamFromFile2->uri:"+ uri);
- String[] proj = { MediaStore.Audio.Media.DATA };
- Cursor cursor = resolver.query(uri,proj,null,null,null);
- int column_index = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
- cursor.moveToFirst();
- String file_path = cursor.getString(column_index);
- Log.i(TAG,"GetFileOutputStreamFromFile2->path:"+ file_path);
- PCMFilePath = file_path;
-
- //openOutputStream
- try {
- out = resolver.openOutputStream(uri);
- }catch (IOException e) {
- e.printStackTrace();
- }
-
- //OutputStream转FileOutputStream
- if (out instanceof FileOutputStream) {
- Fout = (FileOutputStream) out;
- }else{
- Log.e(TAG,"converto FileOutputStream failed.");
- }
- return Fout;
- }
-
- //读文件
- public static FileInputStream GetFileInputStreamFromFile(String fileUrl)
- FileInputStream Fin = null;
- File file_in = new File(fileUrl);
-
- try {
- Fin = new FileInputStream(file_in);
- }catch (IOException e) {
- e.printStackTrace();
- return;
- }
- return Fin;
- }