简体   繁体   English

如何通过MMS或任何社交媒体应用程序从android应用程序发送可绘制图像?

[英]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 我无法从应用程序共享我的图像以作为附件进行消息传递,但我有一组可绘制图像的int数组

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"));

        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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