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

前端 未结 11 1824
隐瞒了意图╮
隐瞒了意图╮ 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

    Check the below code it worked for me

        Picasso.with(getApplicationContext()).load("image url path").into(new Target() {
    
               @Override
    
                public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                    Intent i = new Intent(Intent.ACTION_SEND);
                    i.putExtra(Intent.EXTRA_TEXT, "Let me recommend you this application" +
                            "\n"+ "your share url or text ");
                    i.setType("image/*");
                    i.putExtra(Intent.EXTRA_STREAM, getLocalBitmapUri(bitmap));
                    context.startActivity(Intent.createChooser(i, "Share using"));
                }
    
                @Override
                public void onBitmapFailed(Drawable errorDrawable) {
                }
    
                @Override
                public void onPrepareLoad(Drawable placeHolderDrawable) {
                }
            });
    
    
         private Uri getLocalBitmapUri(Bitmap bmp) {
          Uri bmpUri = null;
          try {
              File file = new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES), "share_image_" + System.currentTimeMillis() + ".png");
            FileOutputStream out = new FileOutputStream(file);
            bmp.compress(Bitmap.CompressFormat.PNG, 50, out);
            out.close();
            bmpUri = Uri.fromFile(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bmpUri;
    }
    
    0 讨论(0)
  • 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"));
    }
    
    0 讨论(0)
  • 2020-11-28 10:06

    I have been looking for solution of this question for a while and found this one up and running, hope it helps.

    private BitmapDrawable bitmapDrawable;
    private Bitmap bitmap1;
    //write this code in your share button or function
    
    bitmapDrawable = (BitmapDrawable) mImageView.getDrawable();// get the from imageview or use your drawable from drawable folder
    bitmap1 = bitmapDrawable.getBitmap();
    String imgBitmapPath= MediaStore.Images.Media.insertImage(getContentResolver(),bitmap1,"title",null);
    Uri imgBitmapUri=Uri.parse(imgBitmapPath);
    String shareText="Share image and text";
    Intent shareIntent=new Intent(Intent.ACTION_SEND);
    shareIntent.setType("*/*");
    shareIntent.putExtra(Intent.EXTRA_STREAM,imgBitmapUri);
    shareIntent.putExtra(Intent.EXTRA_TEXT, shareText);
    startActivity(Intent.createChooser(shareIntent,"Share Wallpaper using"));
    
    0 讨论(0)
  • 2020-11-28 10:07
     String bitmapPath = MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, "title", null);
            Uri bitmapUri = Uri.parse(bitmapPath);
    
            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.setType("image/png");
            intent.putExtra(Intent.EXTRA_STREAM, bitmapUri);
            intent.putExtra(Intent.EXTRA_TEXT, "This is a playstore link to download.. " + "https://play.google.com/store/apps/details?id=" + getPackageName());
    
            startActivity(Intent.createChooser(intent, "Share"));
    
    0 讨论(0)
  • 2020-11-28 10:08
    String text = "Look at my awesome picture";
    Uri pictureUri = Uri.parse("file://my_picture");
    Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.putExtra(Intent.EXTRA_TEXT, text);
    shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
    shareIntent.setType("image/*");
    shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    startActivity(Intent.createChooser(shareIntent, "Share images..."));
    
    0 讨论(0)
  • 2020-11-28 10:10

    It is possibly because the sharing app (FB, twitter, etc) may not have permissions to read the image.

    Google's document says:

    The receiving application needs permission to access the data the Uri points to. The recommended ways to do this are:

    http://developer.android.com/training/sharing/send.html

    I am not sure the sharing apps have permissions to read an image in the bundle of our apps. But my files saved in

     Activity.getFilesDir()
    

    cannot be read. As suggested in the above link, we may consider to store images in the MediaStore, where the sharing apps have permissions to read.

    Update1: The following is working code to share image with text in Android 4.4.

            Bitmap bm = BitmapFactory.decodeFile(file.getPath());
            intent.putExtra(Intent.EXTRA_TEXT, Constant.SHARE_MESSAGE
                + Constant.SHARE_URL);
            String url= MediaStore.Images.Media.insertImage(this.getContentResolver(), bm, "title", "description");
            intent.putExtra(Intent.EXTRA_STREAM, Uri.parse(url));
            intent.setType("image/*");
            startActivity(Intent.createChooser(intent, "Share Image"));
    

    The side effect is we add an image in the MediaStore.

    0 讨论(0)
提交回复
热议问题