Android:从原始文件夹共享图像。错误的图像共享
问题描述:
我有一个Activity
用户可以从raw
文件夹共享图像。Android:从原始文件夹共享图像。错误的图像共享
原始文件夹有70个图像,全部按字母顺序排列。第一个是R.raw.recipe01
,最后一个是R.raw.recipe70
。
我得到的图像int
我想从Bundle
分享,我有一个方法将图像从raw
文件夹复制到可访问的文件。
我打电话startActivity(createShareIntent());
在ActionBar
MenuItem
,它工作成功。
问题
份额intent
将始终选择R.raw.recipe01
的图像,即使从Bundle
的int
是图像〔实施例R.raw.recipe33
。
我已在下面分享我的代码。任何人都可以发现我做错了什么吗?
CODE:
private int rawphoto = 0;
private static final String SHARED_FILE_NAME = "shared.png";
@Override
public void onCreate(Bundle savedInstanceState) {
Bundle bundle = getIntent().getExtras();
rawphoto = bundle.getInt("rawphoto");
int savedphoto = rawphoto;
// COPY IMAGE FROM RAW
copyPrivateRawResourceToPubliclyAccessibleFile(savedphoto);
private Intent createShareIntent() {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/*");
shareIntent.putExtra(Intent.EXTRA_TEXT, "IMAGE TO SHARE: ");
Uri uri = Uri.fromFile(getFileStreamPath("shared.png"));
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
return shareIntent;
}
private void copyPrivateRawResourceToPubliclyAccessibleFile(int photo) {
System.out.println("INT PHOTO: " +photo);
InputStream inputStream = null;
FileOutputStream outputStream = null;
try {
inputStream = getResources().openRawResource(photo);
outputStream = openFileOutput(SHARED_FILE_NAME,
Context.MODE_WORLD_READABLE | Context.MODE_APPEND);
byte[] buffer = new byte[1024];
int length = 0;
try {
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
} catch (IOException ioe) {
/* ignore */
}
} catch (FileNotFoundException fnfe) {
/* ignore */
}
finally {
try {
inputStream.close();
} catch (IOException ioe) {
}
try {
outputStream.close();
} catch (IOException ioe) {
}
}
}
答
删除Context.MODE_APPEND
这样,如果它已经存在的文件被覆盖。
为什么Context.MODE_APPEND? – 2012-08-12 11:39:59
@JoelSjögren我不知道这是为什么,但我已经删除它,它现在起作用。谢谢!将您的评论移至答案,以便我可以接受:-) – tiptopjat 2012-08-12 11:46:11
此问题是我的答案。如何从原始文件夹共享图像。良好的祝福! – 2014-07-28 06:10:38