2013-08-19 4 views
5

로고가있는 응용 프로그램에서 전자 메일을 보내려고합니다.
그러나 문자열 형식의 첨부 파일 (png이어야 함)이 수신되면 이메일을 수신합니다.
내 코드 :
Android에 이미지를 이메일로 첨부 할 수 없습니다.

Intent intent = new Intent(Intent.ACTION_SEND); 
    intent.setType("application/image"); 

    intent.putExtra(Intent.EXTRA_SUBJECT, subject); 
    intent.putExtra(Intent.EXTRA_TEXT, getString(R.string.fb_share_description)); 
    intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("android.resource://my.package/" + R.drawable.ic_launcher)); 
    Intent chooser = Intent.createChooser(intent, "share"); 
    startActivity(chooser); 

어떻게해야합니까?

+0

시나리오를 자세히 설명해 주시겠습니까? 충분히 명확하지 않다 –

+0

매우 간단하다. 파일은 png 형식이 아니다. – NickF

답변

8

내부 리소스에서 이메일에 파일을 첨부 할 수 없습니다. 먼저 SD 카드와 같이 저장소의 일반적으로 액세스 가능한 영역에 복사해야합니다.

InputStream in = null; 
OutputStream out = null; 
try { 
    in = getResources().openRawResource(R.drawable.ic_launcher); 
    out = new FileOutputStream(new File(Environment.getExternalStorageDirectory(), "image.png")); 
    copyFile(in, out); 
    in.close(); 
    in = null; 
    out.flush(); 
    out.close(); 
    out = null; 
} catch (Exception e) { 
    Log.e("tag", e.getMessage()); 
    e.printStackTrace(); 
} 


private void copyFile(InputStream in, OutputStream out) throws IOException { 
    byte[] buffer = new byte[1024]; 
    int read; 
    while ((read = in.read(buffer)) != -1) { 
     out.write(buffer, 0, read); 
    } 
} 

//Send the file 
Intent emailIntent = new Intent(Intent.ACTION_SEND); 
emailIntent.setType("text/html"); 
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "File attached"); 
Uri uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "image.png")); 
emailIntent.putExtra(Intent.EXTRA_STREAM, uri); 
startActivity(Intent.createChooser(emailIntent, "Send mail...")); 

응용 프로그램과 함께 제공되는 리소스가 응용 프로그램에 읽기 전용이며 샌드 박스 처리되어 있기 때문에 필요합니다. 이메일 클라이언트가 수신하는 URI는 액세스 할 수없는 URI입니다.

+0

파일을 자산으로 유지할 수 있습니까? – NickF

+0

+1 잘 설명 ... @ Raghav Sood 우리는 이것을 위해 컨텐트 프로 바이더를 사용할 수 있습니까? –

+0

@NickF 파일을 첨부하기 전에 외부 저장소에 복사하는 한 원하는 위치에 파일을 보관할 수 있습니다. –

관련 문제