有没有办法获取位图的URI而不将其保存到SD卡?

Ahm*_*usa 22 android uri bitmap android-bitmap

我正在制作一个应用程序,允许用户使用android意图共享图像,但如何获取该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);
    }
Run Code Online (Sandbox Code Playgroud)

我需要获取该URI而不将该位图保存到SD卡

Sag*_*wal 18

试试这个:

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

}
Run Code Online (Sandbox Code Playgroud)

  • 您完全忘记了要创建的字节数组.这怎么可能工作? (3认同)