将图像附加到电子邮件Intent使应用程序强制关闭android

时间:2014-02-05 07:34:01

标签: android

我试图用android上的分享按钮发送图像(电子邮件,蓝牙,mms)。 但当我点击其中一个像gmail,应用程序强制关闭它自己 显示错误Log Cat,这是我的代码:

                    Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
                    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "subject");
                    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,new String[]{});
                    emailIntent.setType("text/plain");
                    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,"my text");
                    emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    emailIntent.setType("image/png");
                    Bitmap  mIcon_val =null;
                    URL newurl;
                    try {
                        newurl = new URL(ImageUrl);
                        mIcon_val = BitmapFactory.decodeStream(newurl.openConnection() .getInputStream());
                    } catch (MalformedURLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } 
                    emailIntent.putExtra(Intent.EXTRA_STREAM, mIcon_val);
                    startActivity(Intent.createChooser(emailIntent, getResources().getString(R.string.share)));

错误日志:

02-05 11:10:15.500: E/AndroidRuntime(10724): FATAL EXCEPTION: main
02-05 11:10:15.500: E/AndroidRuntime(10724): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.google.android.gm/com.google.android.gm.ComposeActivity}: java.lang.ClassCastException: android.graphics.Bitmap
02-05 11:10:15.500: E/AndroidRuntime(10724):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
02-05 11:10:15.500: E/AndroidRuntime(10724):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
02-05 11:10:15.500: E/AndroidRuntime(10724):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-05 11:10:15.500: E/AndroidRuntime(10724):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
02-05 11:10:15.500: E/AndroidRuntime(10724):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-05 11:10:15.500: E/AndroidRuntime(10724):    at android.os.Looper.loop(Looper.java:130)
02-05 11:10:15.500: E/AndroidRuntime(10724):    at android.app.ActivityThread.main(ActivityThread.java:3687)
02-05 11:10:15.500: E/AndroidRuntime(10724):    at java.lang.reflect.Method.invokeNative(Native Method)
02-05 11:10:15.500: E/AndroidRuntime(10724):    at java.lang.reflect.Method.invoke(Method.java:507)
02-05 11:10:15.500: E/AndroidRuntime(10724):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
02-05 11:10:15.500: E/AndroidRuntime(10724):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
02-05 11:10:15.500: E/AndroidRuntime(10724):    at dalvik.system.NativeStart.main(Native Method)
02-05 11:10:15.500: E/AndroidRuntime(10724): Caused by: java.lang.ClassCastException: android.graphics.Bitmap
02-05 11:10:15.500: E/AndroidRuntime(10724):    at com.google.android.gm.ComposeArea.initFromExtras(ComposeArea.java:683)
02-05 11:10:15.500: E/AndroidRuntime(10724):    at com.google.android.gm.ComposeActivity.initFromExtras(ComposeActivity.java:1482)
02-05 11:10:15.500: E/AndroidRuntime(10724):    at com.google.android.gm.ComposeActivity.finishOnCreateAfterAccountSelected(ComposeActivity.java:1020)
02-05 11:10:15.500: E/AndroidRuntime(10724):    at com.google.android.gm.ComposeActivity.onCreate(ComposeActivity.java:259)
02-05 11:10:15.500: E/AndroidRuntime(10724):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-05 11:10:15.500: E/AndroidRuntime(10724):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
02-05 11:10:15.500: E/AndroidRuntime(10724):    ... 11 more
02-05 11:10:15.570: E/liblog(1506): failed to call dumpstate
02-05 11:10:15.601: E/(1506): Dumpstate > /data/log/dumpstate_app_error

感谢“Abhishek Agarwal”,这是我的修复和工作:

emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                        emailIntent.setType("image/png");
                        URL url2 = null;
                        try {
                            url2 = new URL (ImageUrl);
                        } catch (MalformedURLException e1) {
                            e1.printStackTrace();
                        }
                        InputStream input = null;
                        try {
                            input = url2.openStream();
                        } catch (IOException e1) {
                            e1.printStackTrace();
                        }
                        File storagePath = null;
                        try {
                            storagePath = Environment.getExternalStorageDirectory();
                            OutputStream output = new FileOutputStream (new File(storagePath,"imgforother.png"));
                            try {
                                byte[] buffer = new byte[500];
                                int bytesRead = 0;
                                while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) {
                                    output.write(buffer, 0, bytesRead);
                                }
                            } catch (IOException e) {
                                e.printStackTrace();
                            } finally {
                                try {
                                    output.close();
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }
                        } catch (FileNotFoundException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } finally {
                            try {
                                input.close();
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }
                        File file = new File(storagePath,"imgforother.png");
                        emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
                        startActivity(Intent.createChooser(emailIntent, getResources().getString(R.string.share)));

1 个答案:

答案 0 :(得分:1)

您在接受ImageURI时在EXTRA_STREAM中发送位图,因此将其更改为uri

     Intent emailIntent = new Intent(Intent.ACTION_SEND);
        emailIntent.setType("image/*");
        emailIntent.putExtra(Intent.EXTRA_SUBJECT, SUBJECT);

        emailIntent.putExtra(Intent.EXTRA_TEXT, TEXT);
        emailIntent.putExtra(Intent.EXTRA_STREAM, savebitmap(bitmap));
        startActivity(Intent.createChooser(emailIntent, "SEnd Via"));

saveBitmap方法

  private Uri savebitmap(Bitmap bmp) {
    String temp="Share";
    String extStorageDirectory =  Environment.getExternalStorageDirectory().toString();
    OutputStream outStream = null;
    String path = Environment.getExternalStorageDirectory()
            .toString();
    new File(path + "/TempFolder").mkdirs();
    File file = new File(path+"/TempFolder", temp + ".png");
    if (file.exists()) {
        file.delete();
        file = new File(path+"/TempFolder", temp + ".png");
    }

    try {
        outStream = new FileOutputStream(file);
        bmp.compress(Bitmap.CompressFormat.PNG, 100, outStream);
        outStream.flush();
        outStream.close();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    return Uri.fromFile(file);
}

在manifest.xml中提供 WRITE_EXTERNAL_STORAGE 权限