从drawable文件夹中附加gmail中的图像

时间:2013-08-09 08:52:43

标签: android email

我的drawable文件夹中有一些图像,还有这些图像的int数组。现在我正试图通过gmail发送电子邮件,附带来自我的drawable文件夹的图像,一切都在android 2.3中正常工作,但在android 4.0中,图像的扩展名缺失。我没有找到任何解决方案。以下是我使用的代码。

Uri imageuri  = Uri.parse("android.resource://MyPackageName/" + SplashActivity.mBitmap_mixed_images[int_image_position]);

            Intent intent = new Intent(Intent.ACTION_SEND); 

               if(imageuri != null){
                intent.putExtra(Intent.EXTRA_STREAM, imageuri);
                intent.putExtra(Intent.EXTRA_SUBJECT, "150+ Reasons to Quit Smoking");
                //intent.putExtra(Intent.EXTRA_TEXT, "www.selftalk.info");
                intent.setType("image/html");
               }else{
                intent.setType("plain/text");
               }
               final PackageManager pm = getPackageManager();
                final List<ResolveInfo> matches = pm.queryIntentActivities(intent, 0);
                ResolveInfo best = null;
                for (final ResolveInfo info : matches)
                  if (info.activityInfo.packageName.endsWith(".gm") ||
                      info.activityInfo.name.toLowerCase().contains("gmail")) best = info;
                if (best != null)
                  intent.setClassName(best.activityInfo.packageName, best.activityInfo.name);
                startActivity(intent);

1 个答案:

答案 0 :(得分:3)

private FileOutputStream outStream; 
private File file;
Bitmap bm = BitmapFactory.decodeResource( getResources(), R.drawable.ic_launcher);
        String extStorageDirectory = Environment.getExternalStorageDirectory().toString();

    file = new File(extStorageDirectory, "ic_launcher.PNG");
    try {
        outStream = new FileOutputStream(file);
        bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);
        outStream.flush();
        outStream.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

email = new Intent(Intent.ACTION_SEND);
email.setType("application/octet-stream");
email.putExtra(Intent.EXTRA_EMAIL, new String[] { "" });
email.putExtra(Intent.EXTRA_SUBJECT, "My Subject");
email.putExtra(Intent.EXTRA_TEXT, "");
email.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
startActivity(Intent.createChooser(email, "Choose an Email client :"));