Reputation: 907
I am trying to read a file from /data partition in Android. I wrote a test App and then started it on the emulator. Then I used "adb push local_path_to_file /data/mydir/file1" to copy file1 under /data/mydir/.
Now I tried the following in my App and nothing happens :(
How to read a file saved in the /data? What is wrong with my code?
Here is the snippet code:
try {
File source = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/data/pu/file1");
InputStream is = new FileInputStream(source);
String fileStr = new String(ReadBytesOfFile(is).toString);
System.out.println("file out ="+ fileStr);
is.close();
}
catch(IOException e){
Log.d("file","NOT FOUND");
}
public byte [] ReadBytesOfFile (InputStream input) throws IOException {
long length = input.available();
byte[] rbuffer = new byte[(int) length];
input.read(rbuffer);
return rbuffer;
}
Upvotes: 0
Views: 300
Reputation: 1006564
First, you cannot store files in /data
in production, except in the location returned to you by getFilesDir()
, so you really need to rethink your approach.
Second, you are not trying to read from /data
. Your code reads from a /data
directory inside of Environment.getExternalStorageDirectory()
. The precise location of Environment.getExternalStorageDirectory()
varies by device and OS version, but /mnt/sdcard
is the most common spot. Hence, you are presently trying to read from something like /mnt/sdcard/data
, not /data
.
Here is the documentation on data storage in Android.
Upvotes: 2