2012-05-21 14 views
6

Sto tentando di allegare un file PDF denominato download.pdf a un'e-mail nella mia app per Android. Sto copiando il file prima su SDCard e poi allegando l'e-mail.Collegamento di un PDF a un'e-mail dall'app per Android - La dimensione del file è zero

Non sono rilevante, ma sto testando un dispositivo con scheda galassia. Il percorso di archiviazione esterno restituisce mnt/sdcard/

Il mio codice è il seguente:

public void sendemail() throws IOException { 

    CopyAssets(); 

    String emailAddress[] = {""}; 

    File externalStorage = Environment.getExternalStorageDirectory(); 

    Uri uri = Uri.fromFile(new File(externalStorage.getAbsolutePath() + "/" + "download.pdf")); 

    Intent emailIntent = new Intent(Intent.ACTION_SEND); 
    emailIntent.putExtra(Intent.EXTRA_EMAIL, emailAddress); 
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject"); 
    emailIntent.putExtra(Intent.EXTRA_TEXT, "Text"); 
    emailIntent.setType("application/pdf"); 
    emailIntent.putExtra(Intent.EXTRA_STREAM, uri); 

    startActivity(Intent.createChooser(emailIntent, "Send email using:")); 

    } 

public void CopyAssets() { 
    AssetManager assetManager = getAssets(); 
    String[] files = null; 
    try { 
     files = assetManager.list(""); 
    } catch (IOException e) { 
     Log.e("tag", e.getMessage()); 
    } 
    for(String filename : files) { 
     InputStream in = null; 
     OutputStream out = null; 
     if (filename.equals("download.pdf")) { 
     try { 
      System.out.println("Filename is " + filename); 
      in = assetManager.open(filename); 
      File externalStorage = Environment.getExternalStorageDirectory(); 
      out = new FileOutputStream(externalStorage.getAbsolutePath() + "/" + filename); 
      System.out.println("Loacation is" + out); 
      copyFile(in, out); 
      in.close(); 
      in = null; 
      out.flush(); 
      out.close(); 
      out = null; 
     } catch(Exception e) { 
      Log.e("tag", e.getMessage()); 
     }  
    } 
    } 
} 
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); 
    } 
} 
} 

Il problema è che il file che si allega è 0 byte. Qualcuno può individuare ciò che potrebbe essere sbagliato?

EDIT

posso vedere che il file è stato salvato sul dispositivo se guardo nelle impostazioni, quindi questo deve essere un problema intorno a come vi allego il file per l'e-mail. Nel Registro errori che sto vedendo:

gMail Attachment URI: file:///mnt/sdcard/download.pdf 
gMail type: application/pdf 
gmail name: download.pdf 
gmail size: 0 

EDIT

Chiedendosi se questo è un bug sul Galaxy Tab? Se apro il file tramite un visualizzatore di file PDF (dalla mia app), quindi provo a collegarlo a un'e-mail di Gmail, la dimensione è nuovamente 0. Qualcuno può verificare?

Grazie.

+0

La vostra applicazione ha il permesso di accedere alla scheda SD? –

+0

Sì - in manifest - GuybrushThreepwood

+0

Hai controllato la dimensione del file pdf nella tua 'sdcard' che hai copiato usando il metodo' copyAssets() '. – Praveenkumar

risposta

0

Se il file download.pdf esiste in SDCard. Quindi il problema dovrebbe essere Uri da File. Prova questo, funziona per me.

Uri uri = Uri.fromFile(new File("/sdcard/", "download.pdf")); 
+0

Non aiuta - non è essenzialmente la stessa cosa che ho sopra? – GuybrushThreepwood

+0

K, cerco di aiutarti, dai un'occhiata qui [cercando di allegare-un-file-da-sd-card-a-email] (http://stackoverflow.com/questions/587917/trying- a-attach-a-file-da-sd-card-to-e-mail). Ti può aiutare –

+0

Grazie, ma ho già trovato quel thread e niente aiuta. – GuybrushThreepwood

0

Lo stesso problema si verifica anche per me. Ho chiarito che usando un metodo dagli esempi. Ho già answered una domanda simile alla tua richiesta. Forse può aiutarti.

0
File externalStorage = Environment.getExternalStorageDirectory();  
String PDFpath = externalStorage.toString(); 
String pdfpath =path.replace("/mnt",""); 
Uri uri = Uri.parse(new File("file://" + pdfpath)); 
6
String[] mailto = {""}; 
         Uri uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/CALC/REPORTS/",pdfname)); 
         Intent emailIntent = new Intent(Intent.ACTION_SEND); 
         emailIntent.putExtra(Intent.EXTRA_EMAIL, mailto); 
         emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Calc PDF Report"); 
         emailIntent.putExtra(Intent.EXTRA_TEXT, ViewAllAccountFragment.selectac+" PDF Report"); 
         emailIntent.setType("application/pdf"); 
         emailIntent.putExtra(Intent.EXTRA_STREAM, uri); 
         startActivity(Intent.createChooser(emailIntent, "Send email using:")); 
Problemi correlati