Android Image sending intent
i am trying to share an image from the drawables in my application. i keep getting java.io.FileNotFoundException:/abc.jpg (read-only file system)
my code is
private String SaveCache(int resID) {
String path = "";
try {
InputStream is = getResources().openRawResource(resID);
File cacheDir = this.getExternalCacheDir();
File downloadingMediaFile = new File(cacheDir, "abc.jpg");
byte[] buf = new byte[256];
FileOutputStream out = new FileOutputStream(downloadingMediaFile);
while (true) {
int rd = is.read(buf, 0, 256);
if (rd == -1 || rd == 0)
break;
out.write(buf, 0, rd);
}
is.close();
out.close();
return downloadingMediaFile.getPath();
} catch (Exception ex) {
Toast.makeText(this, ex.toString(), Toast.LENGTH_LONG).show();
ex.printStackTrace();
}
return path;
}
private void ImageShare() {
String path = SaveCache(R.drawable.testsend);
Toast.makeText(this, path, Toast.LENGTH_LONG).show();
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("image/jpeg");
share.putExtra(Intent.EXTRA_SUBJECT, "test");
share.putExtra(Intent.EXTRA_TEXT, "test");
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + path));
try {
startActivity(Intent.createChooser(share, "Choose share method."));
} catch (Exception ex) {
开发者_开发知识库 ex.printStackTrace();
}
}
Any help is appreciated, if more info is required i will be happy to provide.
Make sure that the external storage is mounted.
From the docs of getExternalCacheDir():
Returns the path of the directory holding application cache files on external storage. Returns null if external storage is not currently mounted so it could not ensure the path exists; you will need to call this method again when it is available.
http://www.anddev.org/post3469.html#p3469
You need to use
FileOutputStream fos = openFileOutput(name, MODE);
as mentioned in the example.
精彩评论