我正在制作一个应用程序,允许用户使用 android Intent 共享图像,但如何获取该 URI
位图无需保存到sd卡
我已经使用了这个代码可以正常工作,但我不需要将这个位图保存到 sd 卡
private Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = Images.Media.insertImage(inContext.getContentResolver(), inImage, "title", null);
return Uri.parse(path);
}
我需要在不将该位图保存到 sd 卡的情况下获取该 URI
最佳答案
试试这个:
protected void ShareImage(Intent intent) {
try {
URL url = new URL(mImageUrl);
Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
intent.putExtra(Intent.EXTRA_STREAM, getImageUri(mActivity, image));
} catch (Exception e) {
e.printStackTrace();
}
startActivityForResult(Intent.createChooser(intent, 1001));
}
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}
public void onActivityResult(int requestCode, int resultCode, Intent data){
//check for result is OK and then check for your requestCode(1001) and then delete the file
}
关于android - 有没有办法在不将其保存到 sdcard 的情况下获取位图的 URI?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26059748/