使用ACTION_SEND意图附加图片 [英] Attaching image using ACTION_SEND intent

查看:155
本文介绍了使用ACTION_SEND意图附加图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个应用程序,在那里我生成使用的EditText现场图像( textArea.setDrawingCacheEnabled(真); textArea.buildDrawingCache(真); ),并将其保存在SD卡上。而在同一时间,我想他的形象来使用ACTION_SEND意图其他应用程序之间共享。在这里,我所面临的问题是,我能够生成的EditText,但相同的图像图像没有得到附加的意图(份额的意图在这个例子中),请告诉我在哪里'我要去错了。

在此先感谢。

 意图份额=新的意图(Intent.ACTION_SEND);
    share.setType(图像/ *);
    字符串状态= Environment.getExternalStorageState();
    如果(Environment.MEDIA_MOUNTED.equals(州)){
        文件picDir =新的文件(Environment.getExternalStorageDirectory()
                +/ myPic);

        如果(!picDir.exists()){
            picDir.mkdir();
        }
        textArea.setDrawingCacheEnabled(真正的);
        textArea.buildDrawingCache(真正的);
        点阵位图= textArea.getDrawingCache();
        日期日期=新的日期();
        字符串文件名=IMG+ date.getTime()+的.png;
        文件picFile =新的文件(picDir +/+文件名);
        尝试 {

            picFile.createNewFile();
            FileOutputStream中picOut =新的FileOutputStream(picFile);
            布尔节省= bitmap.com preSS(比较pressFormat.PNG,100,
                    picOut);

        如果(保存){

                Toast.makeText(
                        getApplicationContext(),
                        图像保存到您的设备中的图片
                                !目录+,Toast.LENGTH_SHORT).show();


                share.putExtra(Intent.EXTRA_STREAM,Uri.parse(Environment.getExternalStorageDirectory()+/+ picFile));
                startActivity(Intent.createChooser(份额,使用发送图片));

            } 其他 {
                //错误
            }
            picOut.close();
        }赶上(例外五){
            e.printStackTrace();
        }
        textArea.destroyDrawingCache();
    } 其他 {
        //错误

    }
 

解决方案

下面是我使用的附加文件以电子邮件的内容:

 意图emailIntent =新的意图(android.content.Intent.ACTION_SEND);
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,getResources()的getString(R.string.lorem));
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,新的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.getAbsolutePath()));
startActivity(Intent.createChooser(emailIntent,getResources()的getString(R.string.send_mail))。);
 

这应该做的伎俩给你。

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

    }

解决方案

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.

这篇关于使用ACTION_SEND意图附加图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆