Android: Action_send Put Extra_stream From Res/drawable Folder Causes Crash
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 s
Solution 1:
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.pngFilefile=newFile (Environment.getExternalStorageDirectory(), "winnerpic.png");
FileOutputStreamoutput=null;
InputStreaminput=null;
try {
output = newFileOutputStream(file);
input = context.getResources().openRawResource(R.drawable.winnerpic);
byte[] buffer = newbyte[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
}
}
}
Post a Comment for "Android: Action_send Put Extra_stream From Res/drawable Folder Causes Crash"