Prova a inviare un file di testo creato come allegato e -mail - da una cartella predefinita

StackOverflow https://stackoverflow.com/questions/8385009

Domanda

Sto provando qualcosa di semplice come creare un file di testo e poi inviarlo come allegato. Mentre funziona bene se utilizzo la SDCard, non so dove inserirla nella "cartella dei dati standard", quindi la mia app funziona effettivamente per tutti senza SDCard (e il file è in qualche modo invisibile)

Mentre questo codice funziona, metto le domande nel <- * commento.

Quando si crea il file:

String FILENAME = "myFile.txt";
    String string = "just something";

    // create a File object for the parent directory
    File myDirectory = new File("/sdcard/myDir/");  // ******** <- what do I have to put HERE for standard data folder????
    // have the object build the directory structure, if needed.
    myDirectory.mkdirs();
    // create a File object for the output file
    File outputFile = new File(myDirectory, FILENAME);
    // now attach the OutputStream to the file object, instead of a String representation
    FileOutputStream fos = null;
   //always have to put try/catch around the code - why? I don't know
    try {
        fos = new FileOutputStream(outputFile);
    } catch (FileNotFoundException e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
    }

     //again have to put try/catch around it - otherwise compiler complains
    try {
        fos.write(string.getBytes());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    }

Quando si invia il file:

public void doSendFile() {
    String fileName = "/sdcard/myDir/myFile.txt"; // ******** <- what do I have to put HERE for standard data folder????
    Intent i = new Intent(Intent.ACTION_SEND);
    try {
        mainDataManager.logFileHandle.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    i.setType("text/plain");
    i.putExtra(Intent.EXTRA_EMAIL, new String[] { "to@someone.com" });
    i.putExtra(Intent.EXTRA_SUBJECT, "subject");
    i.putExtra(Intent.EXTRA_TEXT, "text");
    Log.i(getClass().getSimpleName(),
        "logFile=" + Uri.parse("file://" + fileName));
    i.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + fileName));

    try {
        startActivity(Intent.createChooser(i, "Send mail..."));
    } catch (android.content.ActivityNotFoundException ex) {
        Toast.makeText(getBaseContext(),
            "There are no email clients installed.", Toast.LENGTH_SHORT)
            .show();
    }

    }

Ho scoperto che il file sembra essere archiviato su "Data/Data/com.xxx.xxxx/database/myfile.txt sulla creazione. Ma quando l'ho usato nell'allegato, non è stato inviato nulla.

Quindi, praticamente tutto ciò di cui ho bisogno è sapere come archiviare un file nella memoria locale e poi inviarlo da lì. Dal momento che non tutti potrebbero avere una scheda SD di memoria esterna - suppongo.

Grazie!

È stato utile?

Soluzione

Questo è perché la scheda SD non è protetta, quindi puoi scrivere lì nel modo standart Java come hai fatto tu.

Per scrivere sul file system Android non è possibile utilizzarlo, poiché ogni file è protetto utilizzando una chiave di app per evitare che altre app lo utilizzino.

Shoud usi openFileOutput () Vedi questo tutorial per maggiori informazioni:

http://www.anddev.org/working_with_files-115.html

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top