"Android Code Snippet":
Get any selected file path from gallery,[example used only for png/jpg]
Get any selected file path from gallery,[example used only for png/jpg]
public String getSelectedGalleryImagePath(Intent dataIntent){
String filePath = "";
try {
if(dataIntent == null){
return filePath;
}else{
Uri selectedImage = null;
try {
if(dataIntent.getData().toString().contains(".png")||dataIntent.getData().toString().contains(".jpg")){
filePath = dataIntent.getData().toString()
filePath = filePath.replace("file:///", "");
return filePath;
}else{
selectedImage = Uri.parse(dataIntent.getDataString());
}
} catch (NullPointerException e) {
// TODO: handle exception
selectedImage = null;
}
if(selectedImage == null){
Log.e("mUri"," null");
}else{
Log.e("dataIntent.getDataString()","aa: "+dataIntent.getDataString());
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(
selectedImage, filePathColumn, null, null, null);
if(cursor==null){
Log.e("cursor","is null");
filePath = dataIntent.getDataString();
filePath = filePath.replace("file:///", "");
}else{
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
filePath = cursor.getString(columnIndex);
cursor.close();
}
return filePath;
Log.e("mUri","not null :"+filePath);
}
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
Toast.makeText(this, "Something went wrong.Please check again", Toast.LENGTH_LONG).show();
}
return filePath;
}
No comments:
Post a Comment