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

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

    try using this code.I am uploading ic_launcher from drawable.you can change this with your file from gallary or bitmap.

    void share() {
        Bitmap icon = BitmapFactory.decodeResource(getResources(),
                    R.drawable.ic_launcher);
        Intent share = new Intent(Intent.ACTION_SEND);
        share.setType("image/jpeg");
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        icon.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
        File f = new File(Environment.getExternalStorageDirectory()
                    + File.separator + "temporary_file.jpg");
        try {
            f.createNewFile();
            FileOutputStream fo = new FileOutputStream(f);
            fo.write(bytes.toByteArray());
        } catch (IOException e) {
            e.printStackTrace();
        }
        share.putExtra(Intent.EXTRA_TEXT, "hello #test");
    
        share.putExtra(Intent.EXTRA_STREAM,
                Uri.parse("file:///sdcard/temporary_file.jpg"));            
        startActivity(Intent.createChooser(share, "Share Image"));
    }
    
    0 讨论(0)
  • 2020-11-28 10:17

    By accident(the text message part, I had given up on that), I noticed that when I chose Messages App to handle the request, the Message App would open with the text from Intent.EXTRA_SUBJECT plus the image ready to send, I hope it helps.

    String[] recipient = {"your_email_here"};
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.putExtra(Intent.EXTRA_EMAIL, recipient);
    intent.putExtra(Intent.EXTRA_SUBJECT, "Hello, this is the subject line");
    intent.putExtra(Intent.EXTRA_TEXT, messageEditText.getText().toString());
    intent.putExtra(Intent.EXTRA_STREAM, imageUri);
    intent.setType("image/*");
    
    0 讨论(0)
  • 2020-11-28 10:18

    please have a look on this code worked for me to share an text and image together

    Intent shareIntent;
        Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher);
        String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+"/Share.png";
        OutputStream out = null;
        File file=new File(path);
        try {
            out = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        path=file.getPath();
        Uri bmpUri = Uri.parse("file://"+path);
        shareIntent = new Intent(android.content.Intent.ACTION_SEND);
        shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
        shareIntent.putExtra(Intent.EXTRA_TEXT,"Hey please check this application " + "https://play.google.com/store/apps/details?id=" +getPackageName());
        shareIntent.setType("image/png");
        startActivity(Intent.createChooser(shareIntent,"Share with"));
    

    Don't forget to give WRITE_EXTERNAL_STORAGE Permission

    also in facebook it can only share the image because facebook is not allowing to share the text via intent

    0 讨论(0)
  • 2020-11-28 10:25
    private void shareImage(){
    
        StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
        StrictMode.setVmPolicy(builder.build());
    
        Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.drawable.Starlay_straightface_image);
    
    
        File f =  new File(getExternalCacheDir()+"/"+getResources().getString(R.string.app_name)+".png");
        Intent shareIntent;
    
    
        try {
            FileOutputStream outputStream = new FileOutputStream(f);
            bitmap.compress(Bitmap.CompressFormat.PNG,100,outputStream);
    
            outputStream.flush();
            outputStream.close();
            shareIntent = new Intent(Intent.ACTION_SEND);
            shareIntent.setType("image/*");
            shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f));
            shareIntent.putExtra(Intent.EXTRA_TEXT,"Hey please check this application " + "https://play.google.com/store/apps/details?id=" +getPackageName());
            shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    
    
        }catch (Exception e){
            throw new RuntimeException(e);
        }
        startActivity(Intent.createChooser(shareIntent,"Share Picture"));
    }
    
    0 讨论(0)
  • 2020-11-28 10:28

    you can share plain text by these codes

    String shareBody = "Here is the share content body";
        Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
            sharingIntent.setType("text/plain");
            sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
            sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
            startActivity(Intent.createChooser(sharingIntent, getResources().getString(R.string.share_using)));
    

    so your full code (your image+text) becomes

          private Uri imageUri;
          private Intent intent;
    
                imageUri = Uri.parse("android.resource://" + getPackageName()
                        + "/drawable/" + "ic_launcher");
    
                intent = new Intent(Intent.ACTION_SEND);
    //text
                intent.putExtra(Intent.EXTRA_TEXT, "Hello");
    //image
                intent.putExtra(Intent.EXTRA_STREAM, imageUri);
    //type of things
                intent.setType("*/*");
    //sending
                startActivity(intent);
    

    I just replaced image/* with */*

    update:

    Uri imageUri = Uri.parse("android.resource://" + getPackageName()
            + "/drawable/" + "ic_launcher");
     Intent shareIntent = new Intent();
     shareIntent.setAction(Intent.ACTION_SEND);
     shareIntent.putExtra(Intent.EXTRA_TEXT, "Hello");
     shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
     shareIntent.setType("image/jpeg");
     shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
     startActivity(Intent.createChooser(shareIntent, "send"));
    
    0 讨论(0)
提交回复
热议问题