如何分享我在我的应用程序中拍摄的图像?
How can I share an image that I have taken in my app?
我目前有一个正在拍照的应用程序,我正尝试通过该应用程序直接分享它们。 The problem I'm having is that when the chooser comes up and I pick one, I get an error toast that pops up and says "Unable to attach file".然后它只是让我回到应用程序,其他一切仍然有效。我假设这意味着我必须先保存图像,但我不确定我这样做是否正确。我在网上找到了一些关于如何保存图像的示例,但我不确定这是问题所在还是共享意图是问题所在。
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"));
}
});
我有 "title" 和 "description" 作为占位符,直到我可以让它工作。
尝试替换:
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(Environment.getExternalStorageDirectory().getPath()));
有:
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f));
This question can help you.
我目前有一个正在拍照的应用程序,我正尝试通过该应用程序直接分享它们。 The problem I'm having is that when the chooser comes up and I pick one, I get an error toast that pops up and says "Unable to attach file".然后它只是让我回到应用程序,其他一切仍然有效。我假设这意味着我必须先保存图像,但我不确定我这样做是否正确。我在网上找到了一些关于如何保存图像的示例,但我不确定这是问题所在还是共享意图是问题所在。
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"));
}
});
我有 "title" 和 "description" 作为占位符,直到我可以让它工作。
尝试替换:
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(Environment.getExternalStorageDirectory().getPath()));
有:
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f));
This question can help you.