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
Vectordrawable - Is It Available Somehow for Pre-Lollipop Versions of Android
Any Way to Run Shell Commands on Android Programmatically
Execution Failed for Task ':App:Compiledebugaidl': Aidl Is Missing
Version Conflict Updating to Play-Services 9.4.0 Android Studio 2.2
How to Make a Vertical Seekbar in Android
How to Open a Different Activity on Recyclerview Item Onclick
How Do Detect Android Tablets in General. Useragent
Android Get Orientation of a Camera Bitmap? and Rotate Back -90 Degrees
Installation Failed Due To: 'Null' - Android Studio 3.5
How to Integrate Eclipse with Gradle in Android Project
How to Convert Views to Bitmaps
Get JSONarray Without Array Name
Android/Webkit Text-Overflow: Ellipsis Not Working