亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 Android 10 (Android Q) 中訪問外部存儲?

如何在 Android 10 (Android Q) 中訪問外部存儲?

互換的青春 2024-01-17 20:52:13
我剛剛將源代碼遷移到 Androidx,因為我這樣做了,共享聲音的共享功能不再起作用。Logcat 說:Failed to save file: /storage/emulated/0/appfolder/testsound.mp3 (Permission denied)這是保存聲音的部分:? ? ? ? ? ? final String fileName = soundObject.getItemName() + ".mp3";? ? ? ? ? ? File storage = Environment.getExternalStorageDirectory();? ? ? ? ? ? File directory = new File(storage.getAbsolutePath() + "/appfolder/");? ? ? ? ? ? directory.mkdirs();? ? ? ? ? ? final File file = new File(directory, fileName);? ? ? ? ? ? InputStream in = view.getContext().getResources().openRawResource(soundObject.getItemID());? ? ? ? ? ? try{? ? ? ? ? ? ? ? Log.i(LOG_TAG, "Saving sound " + soundObject.getItemName());? ? ? ? ? ? ? ? OutputStream out = new FileOutputStream(file);? ? ? ? ? ? ? ? byte[] buffer = new byte[1024];? ? ? ? ? ? ? ? int len;? ? ? ? ? ? ? ? while ((len = in.read(buffer, 0, buffer.length)) != -1){? ? ? ? ? ? ? ? ? ? out.write(buffer, 0 , len);? ? ? ? ? ? ? ? }? ? ? ? ? ? ? ? in.close();? ? ? ? ? ? ? ? out.close();? ? ? ? ? ? } catch (IOException e){? ? ? ? ? ? ? ? Log.e(LOG_TAG, "Failed to save file: " + e.getMessage());? ? ? ? ? ? }這是共享聲音的代碼:try{? ? ? ? ? ? ? ? ? ? ? ? ? ? if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1){? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (ActivityCompat.checkSelfPermission(view.getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ActivityCompat.requestPermissions((Activity) view.getContext(), new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }我需要改變什么?如何讓每個人無論使用哪個 Android 版本(minSdkVersion 16)都可以下載和分享聲音?
查看完整描述

2 回答

?
繁星點點滴滴

TA貢獻1803條經驗 獲得超3個贊

如果您的目標平臺為 Android 10 或更高版本,請在應用的清單文件中將 requestLegacyExternalStorage 的值設置為 true:


<manifest ... >

  <!-- This attribute is "false" by default on apps targeting

       Android 10 or higher. -->

  <application android:requestLegacyExternalStorage="true" ... >

    ...

  </application>

</manifest>

訪問文件


(以下兩個代碼塊來自developer.android.com文檔。)


要加載媒體文件,請從ContentResolver調用以下方法之一。


 Uri contentUri = ContentUris.withAppendedId(

        MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,

        cursor.getLong(Integer.parseInt(BaseColumns._ID)));

String fileOpenMode = "r";

ParcelFileDescriptor parcelFd = resolver.openFileDescriptor(uri, fileOpenMode);

if (parcelFd != null) {

    int fd = parcelFd.detachFd();

    // Pass the integer value "fd" into your native code. Remember to call

    // close(2) on the file descriptor when you're done using it.

}

在運行 Android 10(API 級別 29)及更高版本的設備上,您的應用可以使用 IS_PENDING 標志在將媒體文件寫入磁盤時獲得對媒體文件的獨占訪問權限。


ContentValues values = new ContentValues();

values.put(MediaStore.Images.Media.DISPLAY_NAME, "IMG1024.JPG");

values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");

values.put(MediaStore.Images.Media.IS_PENDING, 1);


ContentResolver resolver = context.getContentResolver();

Uri collection = MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY);

Uri item = resolver.insert(collection, values);


try (ParcelFileDescriptor pfd = resolver.openFileDescriptor(item, "w", null)) {

    // Write data into the pending image.

} catch (IOException e) {

    e.printStackTrace();

}


// Now that we're finished, release the "pending" status, and allow other apps

// to view the image.

values.clear();

values.put(MediaStore.Images.Media.IS_PENDING, 0);

resolver.update(item, values, null, null);


查看完整回答
反對 回復 2024-01-17
?
慕運維8079593

TA貢獻1876條經驗 獲得超5個贊

我被困了很長時間,這對我來說效果很好

  StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
    StrictMode.setVmPolicy(builder.build());
    File = new File(filepath);

并且不要忘記在清單文件中請求舊存儲。

  <application android:requestLegacyExternalStorage="true" >


查看完整回答
反對 回復 2024-01-17
  • 2 回答
  • 0 關注
  • 200 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號