1

I have some images in my drawable folder, and also an int array of those images. Now I am trying to send email via gmail with an attached image from my drawable folder, everything is working fine in android 2.3 but in android 4.0 the extension of image is missing. I didn't find any solution. Below is the code I used.

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

1 回答 1

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 :"));
于 2013-08-09T09:47:24.773 回答