getting selected file path from galary/file manager - android

i am trying to get path of selected file but its returning me nothing.... Here is the code i am trying, but i am not being able to figure out the problem
public void getPic() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,
"Select Picture"), SELECT_PICTURE);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
Log.v("IMAGE PATH====>>>> ",selectedImagePath);
}
}
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}

make the change in your getPath() function as follows
public String getPath(Uri uri) {
Cursor cursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
Hope this will help you.. :)

Related

how to select image from Device and it's path in android Studio

I need to select image from device and it's full path
But I get null value.. this is my Code
any suggestions
private void openPicture() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,
"Select Picture"), SELECT_PICTURE);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
String selectedImagePath = getPath(selectedImageUri);
Toast.makeText(this, ""+selectedImagePath, Toast.LENGTH_SHORT).show();
}
}
}
public String getPath(Uri uri) {
if( uri == null ) {
return null;
}
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
if( cursor != null ){
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String path = cursor.getString(column_index);
cursor.close();
return path;
}
return uri.getPath();
}
it's return Null value. I need selected image Path.

Android: Getting path from mediaStore after selecting from android gallery app

I tried to return real photo path stored in my android phone. It works on my 4.4.2 phone but when using 5.0.2 phone, the returned path is null
This is my code getting the real photo path from the intent.getData as a Uri(
content://com.android.providers.media.documents/document/image%3A3061),
String[] proj = { MediaStore.Images.Media.DATA };
CursorLoader loader = new CursorLoader(GlobalApplication.getContext(), photoUri, proj, null, null, null);
Cursor cursor = loader.loadInBackground();
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String result = cursor.getString(column_index);
cursor.close();
return result;
And this is the code I start the gallery application:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),MobileConstant.newInstance().REQUEST_CODE_GALLERY);
I have it working by using this:
Cursor cursor = GlobalApplication.getContext().getContentResolver().query(photoUri, null, null, null, null);
cursor.moveToFirst();
String document_id = cursor.getString(0);
document_id = document_id.substring(document_id.lastIndexOf(":")+ 1);
cursor.close();
cursor = GlobalApplication.getContext().getContentResolver().query(
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
null, MediaStore.Images.Media._ID + " = ? ", new String[]{document_id}, null);
cursor.moveToFirst();
String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
cursor.close();
return path;
//To get picture from gallery or camrea
if (Build.VERSION.SDK_INT < 19) {
Intent intent = new Intent();
intent.setType("image/jpeg");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Complete action using"), 200);
} else {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/jpeg");
startActivityForResult(intent, 300);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//To get result
if (requestCode == 200) {
Uri selectedImage = data.getData();
Log.d("Uri", selectedImage.toString());
startCropImage(selectedImage);
}
else if (requestCode == 300) {
Uri originalUri = data.getData();
final int takeFlags = data.getFlags() & (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
//noinspection ResourceType
getContentResolver().takePersistableUriPermission(originalUri, takeFlags);
Log.d("Uri", originalUri.toString());
startCropImage(originalUri);
}
}
Intent for picking image from gallery
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(intent, reqCode);
In onActivityResult method
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
try {
if (resultCode == Activity.RESULT_OK) {
if (requestCode == reqCode) {
if (data != null) {
String realPath=getRealPathFromURI(data.getData());
}
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private String getRealPathFromURI(Uri contentUri) {
Cursor cursor = null;
try {
String[] proj = { MediaStore.Images.Media.DATA };
cursor = getActivity().getContentResolver().query(contentUri, proj,
null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} finally {
if (cursor != null) {
cursor.close();
}
}
}

Getting Image from gallery in android

I've been trying to get an image from gallery in this app and then on a button click I want to upload to my PHP server.
The "selectedImagePath" comes as null why?
If anyone has a solutions please help me! Thanks in advance.
onCreate()
img = (ImageView) findViewById(R.id.ImageView01);
Button browse = (Button) findViewById(R.id.Button01);
browse.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE);
}
});
onActivityResult()
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
selectedImageUri = data.getData();
System.out.println(selectedImageUri);
selectedImagePath = getPath(selectedImageUri);//returns as null
System.out.println("Image Path : " + selectedImagePath);
img.setImageURI(selectedImageUri);
}
}
}
getPath()
public String getPath(Uri uri) {
String[] projection = {MediaStore.Images.Media.DATA};
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
try this
#Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, 0);
}
#Override
protected void onActivityResult(int reqCode, int resCode, Intent data) {
if(resCode == Activity.RESULT_OK && data != null){
String realPath;
// SDK < API11
if (Build.VERSION.SDK_INT < 11) {
realPath = RealPathUtil.getRealPathFromURI_BelowAPI11(this, data.getData());
}
// SDK >= 11 && SDK < 19
else if (Build.VERSION.SDK_INT < 19) {
realPath = RealPathUtil.getRealPathFromURI_API11to18(this, data.getData());
}
// SDK > 19 (Android 4.4)
else {
realPath = RealPathUtil.getRealPathFromURI_API19(this, data.getData());
}
System.out.println("Image Path : " + realPath);
}
}
public class RealPathUtil {
#SuppressLint("NewApi")
public static String getRealPathFromURI_API19(Context context, Uri uri){
String filePath = "";
String wholeID = DocumentsContract.getDocumentId(uri);
String id = wholeID.split(":")[1];
String[] column = { MediaStore.Images.Media.DATA };
// where id is equal to
String sel = MediaStore.Images.Media._ID + "=?";
Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
column, sel, new String[]{ id }, null);
int columnIndex = cursor.getColumnIndex(column[0]);
if (cursor.moveToFirst()) {
filePath = cursor.getString(columnIndex);
}
cursor.close();
return filePath;
}
#SuppressLint("NewApi")
public static String getRealPathFromURI_API11to18(Context context, Uri contentUri) {
String[] proj = { MediaStore.Images.Media.DATA };
String result = null;
CursorLoader cursorLoader = new CursorLoader(
context,
contentUri, proj, null, null, null);
Cursor cursor = cursorLoader.loadInBackground();
if(cursor != null){
int column_index =
cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
result = cursor.getString(column_index);
}
return result;
}
public static String getRealPathFromURI_BelowAPI11(Context context, Uri contentUri){
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index
= cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
}
the code for getRealPath class is not mine, I din't write down the website.
When you get the data, create file from that path first and then get that file's path as "selectedImagePath". Then you will get actual path of image on external storage: Try below code:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
selectedImageUri = data.getData();
File myFile = new File(selectedImageUri);
System.out.println(selectedImageUri);
selectedImagePath = myFile.getPath();
System.out.println("Image Path : " + selectedImagePath);
img.setImageURI(selectedImageUri);
}
}
}
try this code,
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
System.out.println("Image Path : " + selectedImagePath);
img.setImageURI(selectedImageUri);
}
}
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
Try below code
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri uri = data.getData();
String[] filepath = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(uri, filepath, null,
null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filepath[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
System.out.println("Image Path : " + filePath);
}
I've spent some time on this problem and eventually I found the fully working solution().
A.
It's taken from cordova-plugin-camera which can be found here:
public static String getRealPath(final Uri uri, final Context context) {
String realPath = null;
if (Build.VERSION.SDK_INT < 11) {
realPath = FileHelper.getRealPathFromURI_BelowAPI11(context, uri);
// SDK >= 11
} else {
realPath = FileHelper.getRealPathFromURI_API11_And_Above(context, uri);
}
return realPath;
}
both methods are provided in [FileHelper.class]:
https://github.com/apache/cordova-plugin-camera/blob/06d609cfa4871b4a2811f5a8f7f0a822733209b9/src/android/FileHelper.java
I don't want to duplicate the code, links are provided instead because solutions are quite complex. /I consider the fact that they could expire but hoping they won't/
B.
Here is a link to the source code of a sample project [RealPathUtil.java]: https://github.com/hmkcode/Android/blob/master/android-show-image-and-path/src/com/hmkcode/android/image/RealPathUtil.java
C.
Another question in here that didn't find much attention. [Stack Overflow Post]: https://stackoverflow.com/a/36714242

Getting real path from image Uri is not working on api 19

I am trying to set a LinearLayout background image from Gallery.
To do this I am starting the Image gallery activity ,the URI returning from activity is fine but when I am converting that to a real path I am getting null value at Api 19...
public static String getRealPathFromURI(Context context, Uri contentUri) {
String retVal = null;
if (contentUri != null) {
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = context.getContentResolver().query(contentUri,
proj, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
retVal = cursor.getString(column_index);
}
return retVal;
}
Add android.permission.MANAGE_DOCUMENTS to the manifest
then
private void showImageGallery() {
if (Build.VERSION.SDK_INT < 19) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, GALLERY_INTENT_CALLED);
} else {
showKitKatGallery();
}
}
private void showKitKatGallery(){
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(intent, GALLERY_KITKAT_INTENT_CALLED);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == GALLERY_KITKAT_INTENT_CALLED) {
mChosenImageUri = data.getData();
final int takeFlags = data.getFlags()
& (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
// Check for the freshest data.
getContentResolver().takePersistableUriPermission(mChosenImageUri,takeFlags);
}
}
Try like this
public static String getImgPathFromGallary(final Context context,Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = ((Activity) context).managedQuery(uri, projection, null, null, null);
if (cursor != null) {
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} else
return null;
}

how to get all images and photos from my android device not from sdcard?

i would like to get all images/photos/wallpapers from my android device with image saved path.
I have implemented code for collect the images from sdcard as follows:
String[] mProjection = {
MediaStore.Images.Media._ID,
MediaStore.Images.Media.DATA
};
mCursor = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
mProjection,
null,
null,
MediaStore.Images.Media.DEFAULT_SORT_ORDER);
from the above code i can able to retrive the images from sdcard only.But if the images are available in device phone memory then how can i retrive the images/photos/wallpapers?
If i use INTERNAL_CONTENT_URI it is not returning wallapers info other images information
please any body help me....
Try this answer this will work:
public static final int GALLERY_CODE = 322;
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(
intent,
"Select Picture"),
GALLERY_CODE);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//Assinging on corresponding import
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GALLERY_CODE && resultCode == RESULT_OK) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
try {
//add logic for coping file
} catch (Exception e) {
}
}
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}

Categories

Resources