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;
}
First, you cannot store files in
/datain production, except in the location returned to you bygetFilesDir(), so you really need to rethink your approach.Second, you are not trying to read from
/data. Your code reads from a/datadirectory inside ofEnvironment.getExternalStorageDirectory(). The precise location ofEnvironment.getExternalStorageDirectory()varies by device and OS version, but/mnt/sdcardis 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.