Android:action_send从res / drawable文件夹中输入extra_stream会导致崩溃

时间:2012-04-22 20:49:41

标签: android drawable send

我正在创建一个游戏,并试图让用户通过text / facebook /等分享他们的胜利。我正在使用下面的代码从res / drawable文件夹中获取图像。我很确定我做得对,但是在我选择发送方法(例如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);

1 个答案:

答案 0 :(得分:1)

Android的资源只能通过资源apis访问您的应用程序,文件系统上没有可以通过其他方式打开的常规文件。

您可以做的是将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
        }
    }
}
相关问题