Attaching image using ACTION_SEND intent

2019-04-15 23:43发布

I'm developing an app, where i'm generating an image using EditText field( textArea.setDrawingCacheEnabled(true); textArea.buildDrawingCache(true); ) and saving it on SD card. And at the same time i wanted that image to be shared between other apps using ACTION_SEND intent. Here the problem i'm facing is i'm able to generate the image from EditText but the same image is not getting attached to the intent(share intent in this example), please tell me where i'm going wrong..

Thanks in advance..

Intent share = new Intent(Intent.ACTION_SEND);
    share.setType("image/*");
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        File picDir = new File(Environment.getExternalStorageDirectory()
                + "/myPic");

        if (!picDir.exists()) {
            picDir.mkdir();
        }
        textArea.setDrawingCacheEnabled(true);
        textArea.buildDrawingCache(true);
        Bitmap bitmap = textArea.getDrawingCache();
        Date date = new Date();
        String fileName = "img" + date.getTime() + ".png";
        File picFile = new File(picDir + "/" + fileName);
        try {

            picFile.createNewFile();
            FileOutputStream picOut = new FileOutputStream(picFile);
            boolean saved = bitmap.compress(CompressFormat.PNG, 100,
                    picOut);

        if (saved) {

                Toast.makeText(
                        getApplicationContext(),
                        "Image saved to your device Pictures "
                                + "directory!", Toast.LENGTH_SHORT).show();


                share.putExtra(Intent.EXTRA_STREAM, Uri.parse(Environment.getExternalStorageDirectory() +"/" +picFile));
                startActivity(Intent.createChooser(share, "Send picture using:"));

            } else {
                //Error
            }
            picOut.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        textArea.destroyDrawingCache();
    } else {
        //Error

    }

1条回答
叼着烟拽天下
2楼-- · 2019-04-16 00:22

Here is what I am using for attaching files to an email :

Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,getResources().getString(R.string.lorem));
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,new String[]{emailto});
emailIntent.setType("text/plain");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,getResources().getString(R.string.lorem));
emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
emailIntent.setType("image/jpeg");
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+file.getAbsolutePath()));
startActivity(Intent.createChooser(emailIntent, getResources().getString(R.string.send_mail)));

This should do the trick for you.

查看更多
登录 后发表回答