我有一个Activity,用户可以在其中共享raw文件夹中的图像。
raw文件夹有70张图片,都是按字母顺序命名的。第一个是R.raw.recipe01,最后一个是R.raw.recipe70。
我从Bundle获得了想要共享的图像int,并且我有一个方法可以将图像从raw文件夹复制到一个可访问的文件中。
我在ActionBar MenuItem中调用了startActivity(createShareIntent());,它工作成功。
问题
共享intent将始终选择R.raw.recipe01作为映像,即使来自Bundle的int是用于示例R.raw.recipe33的映像。
我已经在下面分享了我的代码。有人能发现我做错了什么吗?
代码:
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) {
}
}
}发布于 2012-08-12 19:51:56
删除Context.MODE_APPEND,以便在文件已存在时覆盖该文件。
https://stackoverflow.com/questions/11921609
复制相似问题