开发者

How can I write data to file and access it from outside the app (without SD card)?

开发者 https://www.devze.com 2023-04-10 06:49 出处:网络
I am pretty new to Android programming and writing my very first app for my Diploma Thesis. Now I am all but finished but have one thing left that I couldn´t get an answer to anywhere on the net. So

I am pretty new to Android programming and writing my very first app for my Diploma Thesis. Now I am all but finished but have one thing left that I couldn´t get an answer to anywhere on the net. So maybe someone here can help.

Part of what my app must do is write results from previous operations to a freely accessible file for later analysis. So what I got so far is that I am able to write a file to SD card with the following code:

String packageName = this.getClass().getPackage().getName();
String path = Environment.getExternalStorageDirectory().getAbsolutePath()+ "/Android/data/" + packageName + "/files/";
if (isExternalStorageAvailable() && isExternalStorageWritable()) 
{
    String filename = "_results.dat";

    boolean exists = (new File(path)).exists();
    if (!exists) 
    {
         new File(path).mkdirs();
    }
    // Open output stream
    FileOutputStream fOut = new FileOutputStream(path + filename,true);

    fOut.write("lalala".getBytes());
        etc.
     }

public static boolean isExternalStorageAvailable() 
{ 
    // Retrieving the external storage state
    String state = Environment.getExternalStorageState();

    // Check if available
    if (Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) 
    {
        return true;
    }

    return false;
 }


public static boolean isExternalStorageWritable() 
{       
    // Retrieving the external storage state
    String state = Environment.getExternalStorageState();

    // Check if writable
    if (Environment.MEDIA_MOUNTED.equals(state)) 
    {
      return true;
    }

    return false;

 }

That file I can later access, copy, whatever. Like I want it to be. Now the actual question is:

Is there a way to achieve this without an SD card in the phone? Or can I only write into 'app-memory' (which wouldn´t suffice my needs)? I only ask because as of yet there a开发者_JAVA技巧re no SD cards in the phones where I am going to deploy the app. And we have to decide if we have to 'upgrade' the devices :).


Well if you want to avoid having them upgrade the phone with an SD card, check out this link. It SAYS that files written to internal storage are not accessible by other apps, but if you read on, you can set other flags:

MODE_PRIVATE will create the file (or replace a file of the same name) and make it private to your application. Other modes available are: MODE_APPEND, MODE_WORLD_READABLE, and MODE_WORLD_WRITEABLE.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号