如何分享我在应用中拍摄的图像?
问题描述:
我目前有一个应用程序,我正在拍照,我试图直接通过应用程序分享它们。我遇到的问题是,当选择器出现并选择一个时,我弹出一个错误信息,并显示“无法附加文件”。然后它将我带回到应用程序中,其他一切仍然有效。我假设这意味着我必须首先保存图像,但我不确定是否正确执行此操作。我在网上找到了一些关于如何保存图像的例子,但我不确定这是问题还是共享意图是问题。如何分享我在应用中拍摄的图像?
final Button shareButton = (Button) findViewById(R.id.shareButton);
shareButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
imageView.setDrawingCacheEnabled(true);
Bitmap b = imageView.getDrawingCache();
MediaStore.Images.Media.insertImage(getContentResolver(), b, "title", "description");
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/jpeg");
BitmapDrawable bitmapDrawable = (BitmapDrawable) imageView.getDrawable();
Bitmap image = bitmapDrawable.getBitmap();
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File f = new File(Environment.getExternalStorageDirectory() + File.separator + "temp.jpg");
try {
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(Environment.getExternalStorageDirectory().getPath()));
startActivity(Intent.createChooser(shareIntent, "Share Image"));
}
});
作为占位符,我有“标题”和“描述”,直到我能得到它的工作。
答
尝试更换:
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(Environment.getExternalStorageDirectory().getPath()));
有了:
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f));
+0
是的,我实际上已经在另一个选项卡中打开了这个例子,并试图查看与此相比,哪里可能会突破。此修复程序适用于我。谢谢! –
在你的问题中添加错误。 – Pipiks
@Pipiks我没有在android工作室或任何东西的错误,我的应用程序没有崩溃。吐司刚刚弹出说“无法附加文件”,然后它返回到我的应用程序主屏幕,它仍然正常工作。我在帖子中加了一点。 –
好吧,我认为错误的行是'shareIntent.putExtra(Intent.EXTRA_STREAM,Uri.parse(Environment.getExternalStorageDirectory()。getPath()));'你需要在第二个参数中设置图像路径 – Pipiks