Android Share Intent for a Bitmap - is it possible not to save it prior sharing?

前端 未结 5 709
遇见更好的自我
遇见更好的自我 2020-11-27 10:12

I try to export a bitmap from my app using share intent without saving a file for a temporal location. All the examples I found are two-step 1) save to SD Card and create U

5条回答
  •  被撕碎了的回忆
    2020-11-27 10:50

    This for sharing CardView as an Image then saving it in the cache subdirectory of the app's internal storage area. hope it will be helpful.

            @Override
            public void onClick(View view) {
    
                CardView.setDrawingCacheEnabled(true);
                CardView.buildDrawingCache();
                Bitmap bitmap = CardView.getDrawingCache();
    
                try{
                    File file = new File(getContext().getCacheDir()+"/Image.png");
                    bitmap.compress(Bitmap.CompressFormat.PNG,100,new FileOutputStream(file));
                    Uri uri = FileProvider.getUriForFile(getContext(),"com.mydomain.app", file);
    
                    Intent shareIntent = new Intent();
                    shareIntent.setAction(Intent.ACTION_SEND);
                    shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
                    shareIntent.setType("image/jpeg");
                    getContext().startActivity(Intent.createChooser(shareIntent, "Share"));
    
                }catch (FileNotFoundException e) {e.printStackTrace();}
    
            }
        });
    

提交回复
热议问题