I've got problem with this code which is working on windows machine with sdcard connected, but wont work on android. Does anyone know how to read sdcard as a single file byte by byte? I'd like to read all bytes from partition.
public void buttonOnClick(View v) throws IOException {
//File diskRoot = new File("\\\\.\\G:"); // for Windows disk ex. G
int off=0;
int len=1024;
FileInputStream stream = null;
//TODO find direct access to sd card
//File sdDir = new File("storage/extSdCard");
File sdDir = new File("storage/extSdCard");
//File sdDir = new File("dev/block/platform/s3c-sdhci.2/mmcblk1");
//File sdDir = Environment.getExternalStorageDirectory();
//FileInputStream asd = openFileInput(Environment.getExternalStorageState());
//File sdDir = new File(Environment.getExternalStorageDirectory()); gives internal storage of device
//sdDir.getParentFile().mkdirs();
//File file = new File(sdDir,"");
byte[] buffer = new byte[1024];
try {
stream = new FileInputStream(sdDir);
while (stream.read(buffer, off, len) != -1) {
//
}
stream.close();
} catch (FileNotFoundException e){
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Any of commented above versions of "File sdDir" gives in logcat same output:
01-01 21:13:06.751 10887-10887/raf.test W/System.err﹕ java.io.FileNotFoundException: /storage/extSdCard: open failed: EISDIR (Is a directory) 01-01 21:13:06.751 10887-10887/raf.test W/System.err﹕ at libcore.io.IoBridge.open(IoBridge.java:416) 01-01 21:13:06.751 10887-10887/raf.test W/System.err﹕ at java.io.FileInputStream.(FileInputStream.java:78) 01-01 21:13:06.751 10887-10887/raf.test W/System.err﹕ at raf.test.MBR.buttonOnClick(MBR.java:77) 01-01 21:13:06.751 10887-10887/raf.test W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method) 01-01 21:13:06.751 10887-10887/raf.test W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:511) 01-01 21:13:06.751 10887-10887/raf.test W/System.err﹕ at android.view.View$1.onClick(View.java:3719) 01-01 21:13:06.751 10887-10887/raf.test W/System.err﹕ at android.view.View.performClick(View.java:4261) 01-01 21:13:06.751 10887-10887/raf.test W/System.err﹕ at android.view.View$PerformClick.run(View.java:17356) 01-01 21:13:06.751 10887-10887/raf.test W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:615) 01-01 21:13:06.751 10887-10887/raf.test W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:92) 01-01 21:13:06.751 10887-10887/raf.test W/System.err﹕ at android.os.Looper.loop(Looper.java:137) 01-01 21:13:06.751 10887-10887/raf.test W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:4921) 01-01 21:13:06.751 10887-10887/raf.test W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method) 01-01 21:13:06.756 10887-10887/raf.test W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:511) 01-01 21:13:06.756 10887-10887/raf.test W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038) 01-01 21:13:06.756 10887-10887/raf.test W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805) 01-01 21:13:06.756 10887-10887/raf.test W/System.err﹕ at dalvik.system.NativeStart.main(Native Method) 01-01 21:13:06.756 10887-10887/raf.test W/System.err﹕ Caused by: libcore.io.ErrnoException: open failed: EISDIR (Is a directory) 01-01 21:13:06.756 10887-10887/raf.test W/System.err﹕ at libcore.io.IoBridge.open(IoBridge.java:405) 01-01 21:13:06.756 10887-10887/raf.test W/System.err﹕ ... 16 more
Permisions are:
<uses-permission android:name="android.permission.read_external_storage"/>
<uses-permission android:name="android.permission.write_external_storage"/>