2012-04-22 2 views
0

私はゲームを作成していて、ユーザーがtext/facebook/etcで勝利を共有できるようにしようとしています。私は私のres/drawableフォルダからイメージをつかむために、以下のコードを使用しています。私はそれを正しくやっていると確信していますが、sendメソッド(例:Facebook)を選択すると、アプリケーションがクラッシュしています。どんな助けでも大歓迎です。Android:action_send res/drawableフォルダからextra_streamを置くとクラッシュする

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); 
+1

他のアプリはあなたのリソースにアクセスできないため、他のアプリからアクセスできる場所にそのファイルをコピーする必要があります。 – zapl

+0

ああありがとう!今素晴らしい作品:) – SillyFidget

答えて

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 
     } 
    } 
} 
+0

ありがとう。うまくいきました – Shubhank

+0

Rを使ってドロアブルを参照しないと、IDEが警告を出す可能性があります** raw ** .mydrawable – Till

関連する問題