How to Share Image + Text together using ACTION_SEND in android?

前端 未结 11 1883
隐瞒了意图╮
隐瞒了意图╮ 2020-11-28 09:59

I want to share Text + Image together using ACTION_SEND in android, I am using below code, I can share only Image but i can not share Text with it,

private          


        
11条回答
  •  星月不相逢
    2020-11-28 10:04

    To share a drawable image, the image has to be first saved in device's cache or external storage.

    We check if "sharable_image.jpg" already exists in cache, if exists, the path is retrieved from existing file.

    Else, the drawable image is saved in cache.

    The saved image is then shared using intent.

    private void share() {
        int sharableImage = R.drawable.person2;
        Bitmap bitmap= BitmapFactory.decodeResource(getResources(), sharableImage);
        String path = getExternalCacheDir()+"/sharable_image.jpg";
        java.io.OutputStream out;
        java.io.File file = new java.io.File(path);
    
        if(!file.exists()) {
            try {
                out = new java.io.FileOutputStream(file);
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
                out.flush();
                out.close();
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        path = file.getPath();
    
        Uri bmpUri = Uri.parse("file://" + path);
    
        Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
        shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        shareIntent.putExtra(Intent.EXTRA_TEXT, "This is a sample body with more detailed description");
        shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
        shareIntent.setType("image/*");
        startActivity(Intent.createChooser(shareIntent,"Share with"));
    }
    

提交回复
热议问题