【发布时间】:2014-12-03 23:16:23
【问题描述】:
如何在电子邮件中附加图片?我可以在电子邮件中附加文本,但不能正确附加图像, 所以只发送文本而不发送图像。
有问题,
HttpURLConnection urlConnection = (HttpURLConnection) url
.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.connect();
so 控制直接在 urlConnection.connect(); 之后放入 catch 语句,图像不保存在 SDACRD.so 不附加图像。怎么办?
My code in Below,
urlShare = "http://example.com/share.php?id="+ strId;
public class sendImageThroughEmail extends AsyncTask<Void, Void, Void> {
/** Hashmap for Share */
ArrayList<HashMap<String, String>> arrDataList = null;
String strMessage = null, strImageLocator = null;
ProgressDialog progressDialog;
String filePath, strImageName;
protected void onPreExecute() {
progressDialog = new ProgressDialog(getActivity());
progressDialog.setMessage("Please Wait...");
progressDialog.setCancelable(false);
progressDialog.show();
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... arg0) {
arrDataList = new ArrayList<HashMap<String, String>>();
// Retrieve JSON Objects from the given URL address
jsonobject = JSONFunctions.getJSONfromURL(urlShare);
try {
// Locate the array name in JSON
jsonarray = jsonobject.getJSONArray("data");
for (int i = 0; i < jsonarray.length(); i++) {
jsonobject = jsonarray.getJSONObject(i);
strMessage = jsonobject.getString(TAG_MESSAGE);
strImageLocator = jsonobject.getString(TAG_DATA);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
try {
URL url = new URL(strImageLocator);
//URL url = new URL("http://example.com/upload/images (8).jpg");
strImageName = strImageLocator.substring(strImageLocator
.lastIndexOf('/') + 1);
HttpURLConnection urlConnection = (HttpURLConnection) url
.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.connect();
File SDCardRoot = Environment.getExternalStorageDirectory()
.getAbsoluteFile();
String filename = strImageName;
Log.i("Local File:", filename);
File file = new File(SDCardRoot, filename);
if (file.createNewFile()) {
file.createNewFile();
}
FileOutputStream fileOutput = new FileOutputStream(file);
InputStream inputStream = urlConnection.getInputStream();
int totalSize = urlConnection.getContentLength();
int downloadedSize = 0;
byte[] buffer = new byte[1024];
int bufferLength = 0;
while ((bufferLength = inputStream.read(buffer)) > 0) {
fileOutput.write(buffer, 0, bufferLength);
downloadedSize += bufferLength;
Log.i("Progress:", "downloadSize:" + downloadedSize
+ "totalSize:" + totalSize);
}
fileOutput.close();
if (downloadedSize == totalSize) {
filePath = file.getPath();
}
} catch (Exception e) {
e.printStackTrace();
}
Intent email = new Intent(Intent.ACTION_SEND);
File SDCardRoot = Environment.getExternalStorageDirectory()
.getAbsoluteFile();
String filename = strImageName;
File file = new File(SDCardRoot, filename);
Uri markPath = Uri.fromFile(file);
email.putExtra(Intent.EXTRA_STREAM, markPath);
email.putExtra(Intent.EXTRA_SUBJECT, "Share");
email.putExtra(Intent.EXTRA_TEXT, strMessage);
email.setType("image/png");
email.setType("message/rfc822");
startActivity(Intent.createChooser(email, "Choose an Email Client"));
}
};
我的 ImageLocator 像这样, 1)http://example.com/upload/images(8).jpg 2)http://example.com/upload/11_2134_232222_33.png 请指导我。 提前谢谢...
【问题讨论】:
-
您可以使用
EXTRA_TEXT或EXTRA_STREAM,不能同时使用两者,并且setType()调用必须匹配您使用的任何一个。 -
@CommonsWare 但这不是问题。问题是控制放在 urlConnection.connect();然后在直接 catch 语句之后。
-
@CommonsWare 表示未附加。并且不将图像保存在 sdcard 中。
-
那么也许你可以用 LogCat 来看看你的问题是什么:stackoverflow.com/questions/23353173/…
-
email.setType("image/png"); email.setType("message/rfc822");具有顺序破坏性。第二个调用取消了第一个。