我有一个问题,当我想上传很多图片作为附件在电子邮件中,我不能将图片附加到电子邮件中,扩展名不被识别为JPEG(如果我附加单个图片,单个图片成功)。下面是我的源代码:
// imgName is string variable with initialiation above.
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Intent 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, "");
ArrayList<Uri> uris = new ArrayList<Uri>();
for (int i=1;i<=6;i++)
{
Bitmap i1 = null;
int imgID=getBaseContext().getResources().getIdentifier(imgName+"_"+String.valueOf(i), "drawable", getBaseContext().getPackageName());
if (imgID!=0) {
i1 = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), imgID), 200, 200, false);
i1.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] bitmapdata = stream.toByteArray();
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
File file = new File(extStorageDirectory,imgName+"_"+String.valueOf(i));
try {
//file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
fos.write(bitmapdata);
fos.flush();
fos.close();
Uri u = Uri.fromFile(file);
uris.add(u);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
email.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
Intent emailIntent;
emailIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, "");
// emailIntent.setType("application/zip");
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
getString(R.string.ixpenseRecord));
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,
"Images attach...");
ArrayList<Uri> uris = new ArrayList<Uri>();
for (String filepath: arrlistImages) {
File file = new File(filepath);
Uri csvURI = Uri.fromFile(file);
uris.add(csvURI);
}
emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
emailIntent.setType("text/html");
try {
CommonMethods.openGmailAppIntent(this, emailIntent);
} catch (Exception e) {
}
//打开gmail应用程序的另一个方法是下面的代码:
public static void openGmailAppIntent(Context context, Intent intent) {
final PackageManager pm = context.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);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// startActivity(Intent
// .createChooser(emailIntent, "Email to Send"));
try {
context.startActivity(intent);
} catch (ActivityNotFoundException e) {
CommonMethods.showMessageForValiDation(context,
CommonVariable.VALIDATION_NO_GMAIL_APP,
CommonVariable.iXPENSE_APP_FOLDER);
} catch (Exception e) {
}
}
在主代码中,arrlistImages是字符串的数组,它将得到所有的图像路径…然后把这段代码放到你的主活动中。
我希望它对你有用。