2016-07-06 2 views
2

첨부 파일이있는 이메일을 보내려고합니다. 내부 저장 장치의 파일은, 그래서 이것은 내 코드입니다 :첨부 파일이있는 Android 용 이메일을 보내려면 어떻게해야합니까?

File filelocation = new File(getFilesDir().getAbsolutePath()+"/MyApp", "FileName"); 
     Uri path = Uri.fromFile(filelocation); 
     Intent emailIntent = new Intent(Intent.ACTION_SEND); 
     emailIntent .setType("vnd.android.cursor.dir/email"); 
     String to[] = {"[email protected]"}; 
     emailIntent .putExtra(Intent.EXTRA_EMAIL, to); 
     emailIntent .putExtra(Intent.EXTRA_STREAM, path); 
     emailIntent .putExtra(Intent.EXTRA_SUBJECT, "Subject"); 
     startActivity(Intent.createChooser(emailIntent , "Send email...")); 

하지만 난 항상 얻을 : Permission denied for file합니다.

어떻게 해결할 수 있습니까 ?? 매니페스트

에 읽기 권한을 추가 한

+0

Gmail은 5.0 만 외부 저장 http://stackoverflow.com/questions/26883259/gmail-5-0-app-fails-with-permission-denied-for-the-attachment-when에서 파일을 받아 - 받음 –

답변

3

나는이 방법으로 해결했다. 나는 파일을 외부 캐시 디렉토리에 복사하고 보낸다.

File temporaryFile = null; 
    try { 
     temporaryFile = File.createTempFile(keyType.getKeyTypeString(), ".pem", context.getExternalCacheDir()); 
     Utils.copy(new File(getFilesDir().getAbsolutePath()+"/"+ Utils.APP_OPERATOR_DIR, keyType.getKeyTypeString()+".pem"), temporaryFile); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

File filelocation = new File(getFilesDir().getAbsolutePath()+"/MyApp", "FileName"); 
     Uri path = Uri.fromFile(filelocation); 
     Intent emailIntent = new Intent(Intent.ACTION_SEND); 
     emailIntent .setType("vnd.android.cursor.dir/email"); 
     String to[] = {"[email protected]"}; 
     emailIntent .putExtra(Intent.EXTRA_EMAIL, to); 
     emailIntent .putExtra(Intent.EXTRA_STREAM, path); 
     emailIntent .putExtra(Intent.EXTRA_SUBJECT, "Subject"); 
     startActivity(Intent.createChooser(emailIntent , "Send email...")); 
3

문구를 넣을 반드시 사용 - 권한 안드로이드 : 이름 = "android.permission.READ_EXTERNAL_STORAGE을".

-GMail 5.0은 외부 저장소의 파일 만 허용합니다. Gmail 5.0 app fails with "Permission denied for the attachment" when it receives ACTION_SEND intent.

또한이 라이브러리를 사용할 수 있습니다 : compile'com.github.yesidlazaro : GmailBackground : 1.1 '.

String imagePath = data.getStringExtra(GOTOConstants.IntentExtras.IMAGE_PATH); 

    BackgroundMail.newBuilder(ReportBugActivity.this) 
      .withUsername("[email protected]") 
      .withPassword("pages123") 
      .withMailto("[email protected]") 
      .withSubject("Android Bug Report") 
      .withAttachments(imagePath) 
      .withBody("Android Bug Report") 
      .withOnSuccessCallback(new BackgroundMail.OnSuccessCallback() { 
       @Override 
       public void onSuccess() { 
        Toast.makeText(getApplicationContext(), "Email Sent", Toast.LENGTH_LONG).show(); 

        finish(); 
        startActivity(getIntent()); 
       } 
      }) 
      .withOnFailCallback(new BackgroundMail.OnFailCallback() { 
       @Override 
       public void onFail() { 
        Toast.makeText(getApplicationContext(), "Failed", Toast.LENGTH_LONG).show(); 
       } 
      }).send(); 
+0

내 파일이 외부 저장소에 없습니다. 내부 저장소가 – Droide

+0

이고 내부 저장소 사용 권한이 있습니까? android.permission.READ_INTERNAL_STORAGE –

+0

내부 저장소에 대한 허가가 필요 없음을 알았습니다. – Droide

관련 문제