简体   繁体   中英

How to send drawable Images from android app as MMS or on any social media app?

I'm not able to share my image from the app for messaging as an attachment I have a list of int array of drawable images

final int [] imagelistId={
        R.drawable.birthday1,
        R.drawable.birthday2,
        R.drawable.birthday3,
        R.drawable.birthday4,
        R.drawable.birthday5,
        R.drawable.birthday6,
    };

After that i have this code for sharing an image as an attachment

smsBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent smsIntent = new Intent(Intent.ACTION_SENDTO);
                // add the message at the sms_body extra field
                smsIntent.setData(Uri.parse("mmsto:"));
                smsIntent.setType("image/*");
                smsIntent.putExtra("sms_body", "Image");
                smsIntent.putExtra("subject", "Image Message");
                smsIntent.putExtra(Intent.EXTRA_STREAM,Uri.parse("android.resource://com.example.finalgreetings/" + imagelistId[pos]) );
                try{
                    mcontext.startActivity(smsIntent);
                } catch (Exception ex) {
                    Toast.makeText(mcontext, "Your sms has failed...",
                            Toast.LENGTH_LONG).show();
                    ex.printStackTrace();
                }
            }
        });

I have seen many stack overflow questions but none answered my problem. I have also read about converting drawable to bitmap and save to internal or external storage then share it but dont know how to do it. Kindly suggest me best and easy solution. Thanx in advance.

Try this..!!!

smsBtn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {               
                Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.yourimage);//put here your image id
                String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/LatestShare.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);
                Intent shareIntent = new Intent();
                shareIntent = new Intent(Intent.ACTION_SEND);
                shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
                shareIntent.setType("image/png");
                startActivity(Intent.createChooser(shareIntent, "Share with"));

        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM