我有一个 Activity
用户可以在其中分享来自 raw
的图像文件夹。
原始文件夹有 70 张图像,全部按字母顺序命名。第一个是R.raw.recipe01
最后一个是R.raw.recipe70
.
我得到图像 int
我想从 Bundle
分享我有一个方法可以从 raw
复制图像文件夹到可访问的文件。
我调用startActivity(createShareIntent());
在ActionBar
MenuItem
, 成功运行。
问题
分享intent
将始终选择 R.raw.recipe01
作为图像,即使 int
来自 Bundle
例如 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) {
}
}
}
最佳答案
删除 Context.MODE_APPEND
以便文件已存在时被覆盖。
关于java - Android:从原始文件夹共享图像。分享的图片有误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11921609/