適切に他のアプリへの共有描画イメージ(のWhatsAppにPNGを共有するには、FileProviderせずに失敗しました)

owlswipe:

私は(に格納されているユーザーが共有にいくつかの画像のいずれかを選択することができAndroidアプリ作成していdrawableたフォルダを)し、アプリがそれらをサポートするPNGファイルという任意のアプリにそれを共有することができるように、標準ACTION_SENDチューを開き、そのような:

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アプリで画像を開くことを選択した場合、画像が正常に表示されます。

  • これは、複数のデバイス(7.1.1上のPベータとNokia 2上の画素2)で起こっています

  • この問題は、PNG画像は通常の画像(彼らは自動的にのWhatsAppでJPEG画像に変換しているように見えるんだが)のようなのWhatsAppを経由して送信することができます他のアプリ、と発生しません。


私はのWhatsAppが適切なPNGファイルとして私の画像を見て確認するために何ができますか?また、どのように私は正しく、すべてのアプリがそれを正しく解釈することができるように、私のアプリからプリロードされた画像を共有するのですか?

owlswipe:

私はきちんと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(あなたの上でControlキーを押しながらクリックするresのAndroid Studioで新規>ディレクトリに移動します)。その後、内に新しいファイルを作成するxmlと呼ばれるfile_provider_paths(新規にコントロールクリックしxml、ディレクトリ、および新規作成]> [ファイルに移動し、それを名前を付けます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"));

それを取得するにはimageFile、私の中の画像からdrawableディレクトリ、私が最初にそうように、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);
}

そして、設定が完了し、すべてのアプリが今、あなたの共有画像を見ることができます!

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=223299&siteId=1