سؤال

I am trying something simple as creating a text file and then send it as attachment. While it works fine if I use the sdcard I don't know where to put it in the "standard data folder" so my app works actually for everyone without sdcard (and the file is somewhat invisible)

While this code works, I put the questions in the <- * comment.

When creating the 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();
    }
    }

When sending the 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();
    }

    }

I found that the file seems to be stored at "data/data/com.xxx.xxxx/databases/myFile.txt on creation. But when I used this in the attachment, nothing was sent.

So basically all I need is to know how to store a file in local memory and then send it from there. Since not everyone might have an external memory sd card - I assume.

Thanks!

هل كانت مفيدة؟

المحلول

This is becuase the SD card is not protected, so you can write to there in the standart java way as you did.

In order to write to the Android file system you cannot use this, since every file is protected using an App key to avoid other apps to use it.

You shoud use openFileOutput() see this tutorial for more info:

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top