【问题标题】:how to attach image in email in android如何在android中的电子邮件中附加图像
【发布时间】: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_TEXTEXTRA_STREAM,不能同时使用两者,并且setType() 调用必须匹配您使用的任何一个。
  • @CommonsWare 但这不是问题。问题是控制放在 urlConnection.connect();然后在直接 catch 语句之后。
  • @CommonsWare 表示未附加。并且不将图像保存在 sdcard 中。
  • 那么也许你可以用 LogCat 来看看你的问题是什么:stackoverflow.com/questions/23353173/…
  • email.setType("image/png"); email.setType("message/rfc822"); 具有顺序破坏性。第二个调用取消了第一个。

标签: android email-attachments


【解决方案1】:

在您的电子邮件意图中编辑以下字符串:

//...
email.setType("image/jpeg");
email.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+file.getAbsolutePath()));
//...

更多信息您可以查看this answer

编辑

要下载文件,请使用以下代码:

private final static String SD_CARD = Environment
        .getExternalStorageDirectory().getAbsolutePath();
private final static String PNG = ".png";
private final static String APP_FOLDER = "Folder Name";

/**
 * Checking if the SD card is mounted
 * 
 * @return SD card existence
 */
public static boolean isSdPresent()
{
    return Environment.getExternalStorageState().equals(
            Environment.MEDIA_MOUNTED);
}

/**
 * Downloads image file onto SD card in specific folder
 * 
 * @param fileUrl URL for downloading of file
 * @throws IOException
 */
private static void downloadImage(String fileUrl) throws IOException
{
    if (isSdPresent())
    {
        if (fileUrl.length() > 0)
        {
            URL url = new URL(fileUrl);
            InputStream input = url.openStream();

            File folder = new File(SD_CARD, APP_FOLDER);
            if (!folder.exists())
                folder.mkdir();

            OutputStream output = new FileOutputStream(new File(folder,
                    fileUrl.substring(fileUrl.indexOf("=") + 1, fileUrl.length())
                            + PNG));

            byte[] buffer = new byte[1024];
            int bytesRead = 0;
            while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0)
            {
                output.write(buffer, 0, bytesRead);
            }
            output.close();
            input.close();
        }
    }
    else
    {
        if (BuildConfig.DEBUG)
            Log.e("SD card", "not mounted");
    }
}

【讨论】:

  • 是什么让你认为Uri.parse("file://"+file.getAbsolutePath())Uri.fromFile(file); 更好?
  • @njzk2 你好我的控制放 urlConnection.connect();然后直接放入catch语句之后。表示它不会将图像保存在 sdcard 中。如何将图像保存在 sdcard 中,然后在电子邮件中附加图像?
  • @Artyom Kiriliyk 现在不要附加图像,因为图像没有保存在 SD 卡中。请查看我更新的问题。
  • @ArtyomKiriliyk 感谢
猜你喜欢
  • 1970-01-01
  • 2016-12-29
  • 1970-01-01
  • 1970-01-01
  • 2012-03-28
  • 2015-05-15
  • 2012-11-19
  • 2011-04-04
  • 2010-10-29
相关资源
最近更新 更多