Is there a way to get URI of bitmap without saving it to sdcard?

前端 未结 2 1397
小鲜肉
小鲜肉 2020-12-24 07:31

I am making an app that allows the user to share an image using android intent but how to get that URI of

a bitmap without need to saving it to sd card

I ha

相关标签:
2条回答
  • 2020-12-24 07:42

    Try this way

    protected void tryToShareImage(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();
        }
        startActivity(Intent.createChooser(intent, "Share using..."));
    }
    
    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);
    }
    

    after that You can delete files using File.delete()

    0 讨论(0)
  • 2020-12-24 07:43

    Try this:

    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
    
    }
    
    0 讨论(0)
提交回复
热议问题