安卓:ACTION_SEND把extra_stream从RES /文件夹绘制坠毁原因 [英] Android: action_send put extra_stream from res/drawable folder causes crash

查看:187
本文介绍了安卓:ACTION_SEND把extra_stream从RES /文件夹绘制坠毁原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个游戏,并试图允许用户共享通过短信/ Facebook的的/ etc他们的胜利。我使用下面的code抢从我RES /文件夹绘制的图像。我是pretty确保我这样做是正确的,但我选择的发送方法(例如Facebook)的后我的应用程序不断崩溃。任何帮助将大大AP preciated。

 意图ShareIntent =新意图(android.content.Intent.ACTION_SEND);
ShareIntent.setType(图像/ JPEG);
乌里winnerPic = Uri.parse(android.resource://com.poop.pals/+ R.drawable.winnerpic);
ShareIntent.putExtra(Intent.EXTRA_STREAM,winnerPic);
startActivity(ShareIntent);


解决方案

Android的资源只能通过资源的API访问你的应用程序,还有你可以用其他方式打开文件系统没有定期的文件。

你可以做的是把文件复制从的InputStream 你可以在一个地方一个普通文件,该文件是其他应用程序访问。

  //复制R.drawable.winnerpic到/sdcard/winnerpic.png
档案文件=新的文件(Environment.getExternalStorageDirectory(),winnerpic.png);
FileOutputStream中输出= NULL;
输入的InputStream = NULL;
尝试{
    输出=新的FileOutputStream(文件);
    输入= context.getResources()openRawResource(R.drawable.winnerpic)。    字节[]缓冲区=新的字节[1024];
    INT复制;
    而((复制= input.read(缓冲液))!= - 1){
        output.write(缓冲液,0,复制);
    }}赶上(FileNotFoundException异常五){
    Log.e(OMG,不能复制,E);
}赶上(IOException异常五){
    Log.e(OMG,不能复制,E);
} {最后
    如果(输入!= NULL){
        尝试{
            input.close();
        }赶上(IOException异常五){
            // 忽视
        }
    }
    如果(输出!= NULL){
        尝试{
            output.close();
        }赶上(IOException异常五){
            // 忽视
        }
    }
}

I am creating a game, and trying to allow the user to share their win via text/facebook/etc. I am using the code below to grab an image from my res/drawable folder. I am pretty sure I am doing it right, but my app keeps crashing after I choose the send method (ex. facebook). Any help would be greatly appreciated.

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

解决方案

Android's resources are only accessible to your app via the resource apis, there is no regular file on the filesystem you can open in other ways.

What you can do is to copy the file from the InputStream you can get to a regular file in a place that is accessible to other apps.

// 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
        }
    }
}

这篇关于安卓:ACTION_SEND把extra_stream从RES /文件夹绘制坠毁原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆