簡體   English   中英

正確將可繪制圖像共享到其他應用程序(沒有FileProvider的情況下,將PNG共享到Whatsapp失敗)

[英]Share drawable image to other apps properly (sharing PNG to Whatsapp fails without FileProvider)

我正在創建一個Android應用,用戶可以在其中選擇幾張要共享的圖像(存儲在drawable文件夾中),然后該應用會打開一個標准的ACTION_SEND選擇器,以使其可以與支持PNG的任何應用共享,例如所以:

Uri imageUri = Uri.parse("android.resource://com.owlswipe.imagesharer/" + getImage());

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
sendIntent.setType("image/png");
sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(sendIntent, "share to an app"));

public int getImage() {
return R.drawable.firstimage;
}

但是,如果用戶選擇將其共享給Whatsapp,它將無法正常工作:與其將其解釋為圖像(例如,如果您通常與Whatsapp共享照片),不如將其解釋為圖像,並且該文件名為“未命名”作為圖像。

Whatsapp問題

在計算機上打開此“無標題”文檔將發現其名為DOC-20180721-WA0012. 沒有文件擴展名! 手動在文件名的末尾添加png可以顯示正確的圖像。

使這個怪異的(但絕對可以解決!):

  • 例如,如果用戶選擇在SMS應用程序中打開圖像,則圖像會正常顯示。

  • 這發生在多種設備上(P beta上的Pixel 2和7.1.1上的Nokia 2)

  • 在其他應用程序中不會發生此問題,在其他應用程序中,可以通過Whatsapp像正常圖像一樣發送PNG(盡管它們似乎確實由Whatsapp自動轉換為JPEG)。


我該怎么做才能確保Whatsapp將我的圖片視為正確的PNG文件? 另外,如何正確共享應用程序中預加載的圖像,以便每個應用程序都能正確解釋它?

我通過正確實現FileProvider解決了這個問題! 本指南對我有很大幫助 ,但在這里我將做一個簡短的總結。

在清單的application標記中,開始聲明新的FileProvider,如下所示:

<provider
android:name="android.support.v4.content.FileProvider"
android:grantUriPermissions="true"
android:exported="false"
android:authorities="${applicationId}">

    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_provider_paths"/>

</provider>

然后,創建一個名為xml的新目錄(在Android Studio中按住Control鍵單擊您的res ,然后轉到“新建”>“目錄”)。 然后,在xml創建一個名為file_provider_paths的新文件( file_provider_paths鍵並單擊新xml目錄,然后轉到New> File,並將其命名為file_provider_paths.xml )。 將此代碼添加到該新的xml文件中:

<paths>
    <cache-path name="cache" path="/" />
    <files-path name="files" path="/" />
</paths>

最后,在您的MainActivity或類似的地方使用它:

// create new Intent
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);

// set flag to give temporary permission to external app to use your FileProvider
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

// generate URI, I defined authority as the application ID in the Manifest, the last param is file I want to open
Uri uri = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID, imageFile);
intent.putExtra(Intent.EXTRA_STREAM, uri);

// Set type to only show apps that can open your PNG file
intent.setType("image/png");

// start activity!
startActivity(Intent.createChooser(intent, "send"));

為了從drawable目錄中的圖像獲取該imageFile ,我首先將其轉換為Bitmap,然后轉換為File對象,如下所示:

// create file from drawable image
Bitmap bm = BitmapFactory.decodeResource(this.getResources(), R.drawable.yourbeautifulimage);

File filesDir = getApplicationContext().getFilesDir();
File imageFile = new File(filesDir, "ABeautifulFilename.png");

OutputStream os;
try {
    os = new FileOutputStream(imageFile);
    bm.compress(Bitmap.CompressFormat.PNG, 100, os); // 100% quality
    os.flush();
    os.close();
} catch (Exception e) {
    Log.e(getClass().getSimpleName(), "Error writing bitmap", e);
}

完成后,每個應用程序現在都可以看到您的共享圖像!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM