2014-12-02 39 views
1

如何在電子郵件中附加圖片?我可以在電子郵件中附加文本,但不能正確添加圖像,因此只能發送文本但不發送圖像。如何在Android電子郵件中附加圖片

問題的,

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

所以控制直接放於catch語句urlConnection.connect後();,圖像不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 請指導我。 在此先感謝...

+0

您可以讓'EXTRA_TEXT'或'EXTRA_STREAM',而不是兩個,和'的setType()'調用必須匹配無論你使用哪一個。 – CommonsWare 2014-12-02 13:40:28

+0

@CommonsWare但它不是問題。問題是把控制放在urlConnection.connect();然後直接發表聲明。 – Reena 2014-12-02 13:43:43

+0

@CommonsWare的意思是不附加。而不是保存在SD卡中的圖像。 – Reena 2014-12-02 13:44:27

回答

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"); 
    } 
} 
+0

是什麼讓你覺得'Uri.parse(「file://」+ file.getAbsolutePath())'比Uri.fromFile(file);'更好? – njzk2 2014-12-02 15:16:58

+0

@ njzk2你好我的控制把urlConnection.connect();然後直接放入catch語句。意味着它不是將圖像保存在SD卡中。如何在SD卡中保存圖像,然後在電子郵件中附加圖像後? – Reena 2014-12-03 06:20:14

+0

@Artyom Kiriliyk現在不附加圖像,因爲圖像不保存在SD卡中。請看我更新的問題。 – Reena 2014-12-03 06:36:56

相關問題