2012-07-11 45 views
0

我來自stackoverflow的新用戶。我的Android應用程序出現了一些小問題,特別是使用ImageView觸發事件。此事件打開一個預先寫好文本的電子郵件客戶端,它應該附加圖像的圖像。我已經知道圖像應該轉換成位圖之前,然後壓縮併發送到電子郵件客戶端,但不幸的是我不是一個Android/Java專家,所以我無法找到如何做到這一點。這是電子郵件方法的代碼:Android:發送電子郵件,圖像來自ImageView

新的代碼下面

哪裏我不得不更換「字符串imageURI = NULL;」與電子郵件需要的形象。謝謝你們!

編輯:

我設法修改我的代碼,這個,那個沒有給出錯誤:

public void sendMail(ImageView image){ 
    Intent i = new Intent(Intent.ACTION_SEND); 
    int imageURI = R.drawable.img1; 

    i.setType("text/plain"); 
    i.putExtra(Intent.EXTRA_EMAIL , new String[]{"[email protected]"}); 
    i.putExtra(Intent.EXTRA_SUBJECT, "Oggetto"); 
    i.putExtra(Intent.EXTRA_TEXT , "Globelife"); 
    i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 
    i.setType("image/jpeg"); 
    i.putExtra(Intent.EXTRA_STREAM, Uri.parse("android.resource://"+getPackageName()+"/"+imageURI)); 


    try { 
     startActivity(Intent.createChooser(i, "Send mail...")); 
    } catch (android.content.ActivityNotFoundException ex) { 
     Toast.makeText(Test01Activity.this, "Non sono presenti app per l'invio di e-mails.", Toast.LENGTH_SHORT).show(); 
    } 

} 

但我需要改變 「INT imageURI = R.drawable.img1;」到「int imageURI = ImageView.src;」或類似的東西

+0

要發送的圖像保存在本地SD卡上,還是來自Internet? – 2012-07-11 08:30:16

+0

圖像是在應用程序的可繪製文件夾中 – Stefano 2012-07-11 09:22:28

回答

4

試試這個

ImageView iv = (ImageView) findViewById(R.id.splashImageView); 
Drawable d =iv.getBackground(); 
BitmapDrawable bitDw = ((BitmapDrawable) d); 
Bitmap bitmap = bitDw.getBitmap(); 
File mFile = savebitmap(bitmap); 

然後

Uri u = null; 
    u = Uri.fromFile(mFile); 

    Intent emailIntent = new Intent(Intent.ACTION_SEND); 
    emailIntent.setType("image/*"); 
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Hello..."); 
    // + "\n\r" + "\n\r" + 
    // feed.get(Selectedposition).DETAIL_OBJECT.IMG_URL 
    emailIntent.putExtra(Intent.EXTRA_TEXT, "Your tsxt here"); 
    emailIntent.putExtra(Intent.EXTRA_STREAM, u); 
    startActivity(Intent.createChooser(emailIntent, "Send email...")); 

savebitmap方法

private File savebitmap(Bitmap bmp) { 
    String extStorageDirectory = Environment.getExternalStorageDirectory().toString(); 
    OutputStream outStream = null; 
    File file = new File(extStorageDirectory, temp + ".png"); 
    if (file.exists()) { 
    file.delete(); 
    file = new File(extStorageDirectory, temp + ".png"); 
    } 

    try { 
    outStream = new FileOutputStream(file); 
    bmp.compress(Bitmap.CompressFormat.PNG, 100, outStream); 
    outStream.flush(); 
    outStream.close(); 
    } catch (Exception e) { 
    e.printStackTrace(); 
    return null; 
    } 
    return file; 
} 
+0

它說在最後一行代碼(「i.putExtra(Intent.EXTRA_STREAM,bis);」)錯誤「The method putExtra(String,boolean)in類型的意圖不適用於參數(字符串,ByteArrayInputStream)「 – Stefano 2012-07-11 09:33:40

+0

@Stefano我的壞..我已經編輯我的答案..現在試試.. – 2012-07-11 09:43:12

+0

你的代碼沒有給出編譯錯誤,但當我點擊圖像到發送電子郵件時,它會在sendMail(ImageView圖像)(我們正在處理的那個)方法上給出NullPointerException。我正在嘗試搜索什麼是錯誤的。 – Stefano 2012-07-11 09:54:48

-1

//重新移動String imageURI=null;

public void sendMail(ImageView image){ 
    Intent i = new Intent(Intent.ACTION_SEND); 

Uri pngImageUri = Uri.parse(image); 


i.setType("image/png");//change here with image/png 
    i.putExtra(Intent.EXTRA_EMAIL , new String[]{"[email protected]"}); 
    i.putExtra(Intent.EXTRA_SUBJECT, "Oggetto"); 
    i.putExtra(Intent.EXTRA_TEXT , "Testo"); 
    i.putExtra(Intent.EXTRA_STREAM, pngImageUri); 
+0

再次您好,我試圖使用「Uri.parse(圖像);」但它說 「Uri類型中的方法解析(字符串)不適用於參數(ImageView)」 – Stefano 2012-07-11 08:30:09

+0

然後使用'Uri.fromFile(image);' – 2012-07-11 08:33:55

+0

它說現在有同樣的錯誤,現在用File(「Uri類型的文件(File)中的方法不適用於參數(ImageView)」) – Stefano 2012-07-11 08:38:05

0
Intent intent=new Intent(Intent.ACTION_SEND); 
String[] recipients={"[email protected]"}; 
intent.putExtra(Intent.EXTRA_EMAIL, recipients); 
intent.putExtra(Intent.EXTRA_SUBJECT, "Oggetto"); 
intent.putExtra(Intent.EXTRA_TEXT , "Testo"); 
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 
intent.setType("image/png"); 
intent.putExtra(Intent.EXTRA_STREAM,Uri.parse(「file///sdcard/Images/your_image.jpg」));//or you can pass the path of your image 
startActivity(Intent.createChooser(intent, "Send mail")); 
0
Intent i = new Intent(Intent.ACTION_SEND); 
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
i.setType("image/jpg"); 
i.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/Pictures/ 
image.jpg")); 
startActivity(i);