如何共享未存储在设备上的图像?
How to share images that are not stored on device?
我有一个应用程序,我将来自用户的文本转换为 QR 码并在 imageView 中显示 QR 码。我的问题是我如何在此图像上使用共享 Intent,因为它们没有为其指定路径,图像也没有存储在任何地方?
您可以通过将图片的位图保存在缓存中然后共享来实现。
//To get the bitmap from Imageview
imageView.setDrawingCacheEnabled(true);
Bitmap bitmap = imageView.getDrawingCache();
//To share it via Intent**
try {
File file = new File(getContext().getCacheDir(), fileName + ".png");
FileOutputStream fOut = new FileOutputStream(file);
bitmap.compress(CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
file.setReadable(true, false);
final Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
intent.setType("image/png");
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
我有一个应用程序,我将来自用户的文本转换为 QR 码并在 imageView 中显示 QR 码。我的问题是我如何在此图像上使用共享 Intent,因为它们没有为其指定路径,图像也没有存储在任何地方?
您可以通过将图片的位图保存在缓存中然后共享来实现。
//To get the bitmap from Imageview
imageView.setDrawingCacheEnabled(true);
Bitmap bitmap = imageView.getDrawingCache();
//To share it via Intent**
try {
File file = new File(getContext().getCacheDir(), fileName + ".png");
FileOutputStream fOut = new FileOutputStream(file);
bitmap.compress(CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
file.setReadable(true, false);
final Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
intent.setType("image/png");
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}