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

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

使用文件提供程序檢索內容 URI 失敗

使用文件提供程序檢索內容 URI 失敗

神不在的星期二 2022-12-15 16:44:59
我花了幾天時間閱讀文檔并遵循各種教程,但無論我做什么,我都會遇到錯誤。我正在嘗試將圖像保存在內部存儲中,然后將其轉換為內容 Uri,以便我可以使用系統裁剪和調整大小功能。如果有人能發現我的代碼哪里有問題,我將不勝感激:主要活動:new Thread(() -> {try {    String temp = "temp";    //Convert bitmap to byte array    ByteArrayOutputStream bos = new ByteArrayOutputStream();    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);    byte[] bitmapData = bos.toByteArray();    //Write the bytes in file    FileOutputStream fos = openFileOutput(temp, MODE_PRIVATE);    fos.write(bitmapData);    fos.flush();    fos.close();    //onvert file to contentUri    File file = new File(getFilesDir(), temp);    Intent intent = new Intent();    intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);    intent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);    Uri imageUri = FileProvider.getUriForFile(MainActivity.this, "@string/file_provider_authority", file);    intent.setDataAndType(imageUri, "image/jpg");    intent = WallpaperManager.getInstance(getApplicationContext()).getCropAndSetWallpaperIntent(imageUri);    startActivityForResult(intent, ACTIVITY_CROP);} catch (Exception e) {    e.printStackTrace();}}).start();顯現:<provider        android:name="android.support.v4.content.FileProvider"        android:authorities="@string/file_provider_authority"        android:grantUriPermissions="true"        android:exported="false">        <meta-data            android:name="android.support.FILE_PROVIDER_PATHS"            android:resource="@xml/file_provider_paths" />    </provider></application>file_provider_paths.xml:<paths><files-path    path="/" name="temp" />字符串.xml:<resources><string name="file_provider_authority"    translatable="false">com.me.wallpaper_app.provider</string>
查看完整描述

2 回答

?
繁花如伊

TA貢獻2012條經驗 獲得超12個贊

"@string/file_provider_authority"不是您在 Java 中引用字符串資源的方式。使用getString(R.string.file_provider_authority)。

更新:根據評論,第二個主要問題是此內容的使用者沒有使用 的 MIME 類型Intent,而是調用getType()aContentResolver以從ContentProvider. 在這種情況下,那是您的FileProvider,并且FileProvider只知道如何使用文件擴展名并將其轉換為 MIME 類型。因此,usingtemp.jpeg提供FileProvider了返回正確 MIME 類型所需的信息。



查看完整回答
反對 回復 2022-12-15
?
慕斯王

TA貢獻1864條經驗 獲得超2個贊

嘗試這個


      Uri imageUri = FileProvider.getUriForFile(MainActivity.this, R.string.file_provider_authority, file);

也改變


    <paths>

    <files-path

    path="/" name="temp" />


    <?xml version="1.0" encoding="utf-8"?>

    <paths xmlns:android="http://schemas.android.com/apk/res/android"> 

    <external-path name="external_files" path="."/>

    </paths>


查看完整回答
反對 回復 2022-12-15
  • 2 回答
  • 0 關注
  • 137 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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