How to attach a Bitmap when launching ACTION_SEND intent

后端 未结 4 1087
無奈伤痛
無奈伤痛 2020-12-05 00:44

I have this code:

 Intent intent = new Intent(); 
 intent.setAction(Intent.ACTION_SEND); 
 startActivity(intent); 

Which will successfully

4条回答
  •  广开言路
    2020-12-05 01:17

    You must first save the bitmap to a file. you can save it to the app's cache

    private void shareBitmap (Bitmap bitmap,String fileName) {
        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();
        }
    
    }
    

提交回复
热议问题