0

私はゲームを作成していて、ユーザーがtext / facebook/etcを介して勝利を共有できるようにしようとしています。以下のコードを使用して、res/drawableフォルダーから画像を取得しています。私はそれを正しく行っていると確信していますが、sendメソッド(例:facebook)を選択した後もアプリがクラッシュし続けます。どんな助けでも大歓迎です。

Intent ShareIntent = new Intent(android.content.Intent.ACTION_SEND);
ShareIntent.setType("image/jpeg");
Uri winnerPic = Uri.parse("android.resource://com.poop.pals/" + R.drawable.winnerpic);
ShareIntent.putExtra(Intent.EXTRA_STREAM, winnerPic);
startActivity(ShareIntent);
4

1 に答える 1

1

Androidのリソースには、リソースAPIを介してのみアプリにアクセスできます。ファイルシステムには、他の方法で開くことができる通常のファイルはありません。

あなたができることは、InputStream他のアプリがアクセスできる場所にある通常のファイルにアクセスできるファイルをコピーすることです。

// copy R.drawable.winnerpic to /sdcard/winnerpic.png
File file = new File (Environment.getExternalStorageDirectory(), "winnerpic.png");
FileOutputStream output = null;
InputStream input = null;
try {
    output = new FileOutputStream(file);
    input = context.getResources().openRawResource(R.drawable.winnerpic);

    byte[] buffer = new byte[1024];
    int copied;
    while ((copied = input.read(buffer)) != -1) {
        output.write(buffer, 0, copied);
    }

} catch (FileNotFoundException e) {
    Log.e("OMG", "can't copy", e);
} catch (IOException e) {
    Log.e("OMG", "can't copy", e);
} finally {
    if (input != null) {
        try {
            input.close();
        } catch (IOException e) {
            // ignore
        }
    }
    if (output != null) {
        try {
            output.close();
        } catch (IOException e) {
            // ignore
        }
    }
}
于 2012-07-19T11:39:04.200 に答える