Reading a Specific File from Sdcard in Android

reading a specific file from sdcard in android

You are trying to read a directory... what you need is the file! Do something like this... then, you can read the file as you want.

File dir = Environment.getExternalStorageDirectory();
File yourFile = new File(dir, "path/to/the/file/inside/the/sdcard.ext");

Standard way to read a file from sdcard

I suppose you wish access removable SDCard . The standard way to get the path of that SDCard is: Context.getExternalFilesDirs() . Normally SDCard path is the last one and you have to strip out the private subpath (Android/...) . Better check if SDCard is mounted too.

String path = null;
File[] dirs = getContext().getExternalFilesDirs(null);
if (dirs!=null && Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
path = dirs[dirs.length-1].getAbsolutePath();
path = path.substring(0, path.indexOf("Android"));
}
return path;

Reading files from SD card issue: no file is visible

If your phone has android 6.0 or newer version, you have to ask run time permissions. More info you can get from https://developer.android.com/training/permissions/requesting.html

How to read a CSV file from a SD card using Android

You should check the below link which has already been put up here, before some time.

Link is:

reading a specific file from sdcard in android

read data from sdcard in android

Maybe I've missed it in your code but I could not spot any Intent in it. You have to call an Intent with the ACTION_VIEW flag for whatever file you want to be shown.

For instance.

Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri imgUri = Uri.parse("file://" + file.getPath());
intent.setDataAndType(imgUri, "image/*");
startActivity(intent);

You simple create an instance of Intent, set the action which is ACTION_VIEW in our case. Then you create an Uri object by concatenating the path of your file object to file://. All you have to do now is setting the data and type on the intent by specifying the uri and a type string. In my example every image type. You could however just specify a certain image type. Once your intent is set up and ready you fire it off by starting an Activity with the intent as a parameter.

Android is going to take care of finding an appropriate application to display the data in the intent.



Related Topics



Leave a reply



Submit