我有问题,当我想在电子邮件中上传许多图片作为附件时,我无法将图片附加到电子邮件中,也不能将附加内容识别为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);
答案 0 :(得分:1)
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是字符串的集合体,它将获取所有图像路径...然后将此代码放入主活动中。
我希望它对你有用。