Balaji Sabdhar M
Balaji Sabdhar M

Reputation: 428

Small issue over DDMS data folder

I am new to android . So i was trying to implement a code which stores the data in the internal storage of the device. It is storing and loading the data. I cannot see the file in the data folder which is present in the eclipse. When i click the data folder , it is not expanding. The data folder is clicked but could not see any files inside it

I have used the below code

package com.example.files;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
    EditText textBox;
    static final int READ_BLOCK_SIZE = 100;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textBox = (EditText) findViewById(R.id.txtText1);
    }
    public void onClickSave(View view){
        String str = textBox.getText().toString();
        try
        {
            FileOutputStream fOut =
                    openFileOutput("textfile.txt",
                            MODE_WORLD_READABLE);
            OutputStreamWriter osw = new
                    OutputStreamWriter(fOut);
            //---write the string to the file---
            osw.write(str);
            osw.flush(); 
            osw.close();
            //---display file saved message---
            Toast.makeText(getBaseContext(),
                    "File saved successfully!",
                    Toast.LENGTH_SHORT).show();
            //---clears the EditText---
            textBox.setText("");
        }
        catch (IOException ioe)
        {
            ioe.printStackTrace();
        }
    }
    public void onClickLoad(View view) {
        try
        {
            FileInputStream fIn = 
                    openFileInput("textfile.txt");
            InputStreamReader isr = new 
                    InputStreamReader(fIn);
            char[] inputBuffer = new char[READ_BLOCK_SIZE];
            String s = "";
            int charRead;
            while ((charRead = isr.read(inputBuffer))>0)
            {
                //---convert the chars to a String---
                String readString =
                        String.copyValueOf(inputBuffer, 0,
                                charRead);
                s += readString;
                inputBuffer = new char[READ_BLOCK_SIZE];
            }
            //---set the EditText to the text that has been 
            // read---
            textBox.setText(s);
            Toast.makeText(getBaseContext(),
                    "File loaded successfully!",
                    Toast.LENGTH_SHORT).show();
        }
        catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
}

Upvotes: 1

Views: 421

Answers (2)

Mobility
Mobility

Reputation: 263

The data folder is part of the internal storage of the device and should not be accessible over DDMS for a device.

You can however run your app on the emulator and then you can browse the emulator's data directory in DDMS.

The apps in all new smartphone OS's work on the concept of sandboxing. In case of Android the sandbox directory for an app is /data/data/your_app_package/

The files and directories in this directory are by default only available to the app. If you want to view the files using DDMS on a device, I suggest you get a handle on the External Storage using getExternalStorage() and copy/move your files there. The default location to save your files in the app is getFilesDir() which /data/data/your_app_package/files

Upvotes: 3

weston
weston

Reputation: 54781

The constant MODE_WORLD_READABLE is deprecated.

If you want to write a file that can be seen externally, then use Environment.getExternalFilesDir

Note it doesn't mean the file will be on external storage.

Upvotes: 0

Related Questions