如何在android中的电子邮件中附加图像

时间:2014-12-02 13:32:35

标签: android email-attachments

如何在电子邮件中附加图片?我可以在电子邮件中附加文字但不能正确附加图像,       所以只发送文本但不发送图像。

问题,

HttpURLConnection urlConnection = (HttpURLConnection) url
                        .openConnection();
                urlConnection.setRequestMethod("GET");
                urlConnection.setDoOutput(true);
                urlConnection.connect();

所以在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 请指导我。 提前谢谢......

1 个答案:

答案 0 :(得分:0)

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

//...
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");
    }
}