I want to pick file that exist in sdcard not in internal storage and upload it server but I am not able to get its path for getting its size. I have start an intent for pick a file using below code:
intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
String[] mimetypes = {"application/*"};
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
startActivityForResult(
Intent.createChooser(intent, "Select a File to Upload"), FILE_SELECT_CODE);
For getting path to file I am using this answer and its working unless user select any file from sdcard(removable). When I debug the code and found that type is not primary so it will not go inside this condition :
if("primary".equalsIgnoreCase(type)){
return Environment.getExternalStorageDirectory() + "/" + split[1];
}
So my question is what will be it's else? i.e. what if type is not primary? how we can get path to file in that case? I have search many questions and tutorial there is no else in any of them. I have also tried else part of this answer but its not working because System.getenv()
return null for "SECONDARY_STORAGE" and sdcard for "EXTERNAL_STORAGE". I get file not found exception when I try:
if ("primary".equalsIgnoreCase(type)) {
return Environment.getExternalStorageDirectory() + "/" + split[1];
}else{
return System.getenv("EXTERNAL_STORAGE") + "/" + split[1];
}
Uri and doc Id for file look like:
Uri: content://com.android.externalstorage.documents/document/0EF9-3110%3Adevice-2016-12-02-130553.png
docId: 0EF9-3110:device-2016-12-02-130553.png
Any help ??