Selected Image Not Returned From MediaStore - android

Based on code suggestions here at stackoverflow, I've tried to extract an image from the MediaStore. However, when I select an actual photo, getContentResolver().query() always returns null. I can't understand why...
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(i, SELECT_PHOTO);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.v(TAG,"onActivityResult: requestCode = "+requestCode+", resultCode = "+requestCode);
if (requestCode == SELECT_PHOTO) {
Uri selectedImage = data.getData();
String[] filePathColumn = {android.provider.MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
if (cursor != null) {
Log.v(TAG,"onActivityResult: count = "+cursor.getCount());
if (cursor.getCount() == 1) {
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
Log.v(TAG,"onActivityResult: filePath = "+filePath);
}
cursor.close();
}
}
}

try restart your emulater or your device(or uninstall/install the SD card)?
somebody tell me the Android System will scan the medias when restart or reload SD card...maybe the database hasn't change.

Related

android loading image from sd card into db

I need to create a view to select a photo and store that photo in my local db.
I have tried:
private final int SELECT_IMAGE = 1;
In a button I put:
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, SELECT_IMAGE);
But when I select the picture, the intent close and dont return to my apk.
Some here, my intent is finish. And the onActivityResult never runs. So, i can't handler...
My onActivityResult is this:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode){
case SELECT_IMAGE:
if(resultCode==RESULT_OK){
Uri uri = data.getData();
String[] projection = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
if(cursor.moveToFirst()){
int columnIndex = cursor.getColumnIndex(projection[0]);
String filePath = cursor.getString(columnIndex);
}
cursor.close();
}
}
}
And to help, on LogCat I have NOTHING. Amazing...
I am using apk on API 7.

Real File Path Not Appearing

I asked this previous question and have edited it to suit my code for a Video Player running from an intent
videoURI = getIntent().getData();
vv.setVideoURI(videoURI);
but this gives me a blank string when I test it with setText()
How do I get this to work so I can get the full path for the launched video? EG. /storage/extSdCard/Videos/Video.mp4
My Code:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Uri uri = data.getData();
String[] filePathColumn = { MediaStore.Video.Media.DATA };
Cursor cursor = getContentResolver().query(uri,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
path = cursor.getString(columnIndex);
cursor.close();
}
Use MediaStore.Video.VideoColumns.DATA instead of MediaStore.Video.Media.DATA

URI VS ACTION_GET_CONTENT

When to use
// to get image randomly
public static Uri getRandomImage(ContentResolver resolver) {
String[] projection = new String[] { BaseColumns._ID
};
Uri uri = new Random().nextInt(1) == 0 ? Media.EXTERNAL_CONTENT_URI
: Media.INTERNAL_CONTENT_URI;
Cursor cursor = Media.query(resolver, uri, projection, null,
MediaColumns._ID);
if (cursor == null || cursor.getCount() <= 0) {
return null;
}
cursor.moveToPosition(new Random().nextInt(cursor.getCount()));
return Uri.withAppendedPath(uri, cursor.getString(0));
}
and when to use ACTION_GET_CONTENT intent ?
as I want to pick an Image from an android device !
PLease help
If you want to pick an image from teh android gallery then try something like this :
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 100);
and to get back the result you could do something like this :
protected void onActivityResult(int requestCode, int resultCode,
Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch(requestCode) {
case REQ_CODE_PICK_IMAGE:
if(resultCode == RESULT_OK){
Uri selectedImage = imageReturnedIntent.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(
selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
}
}
}
You could also refer to this :
How to pick an image from gallery (SD Card) for my app?

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);
}

Choose image from image and get the directory returned

I have to launch the image gallery of Android and let the user to select an image. So I want that after the selection, the directory image is returned.
How can I do it?
Thanks.
Michele,
To launch the Intent for image selection from the Gallery, use the following code:
public void imageFromGallery() {
Intent getImageFromGalleryIntent =
new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(getImageFromGalleryIntent, SELECT_IMAGE);
}
Then, once the user has made their selection you get the result in onActivityResult() like so:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
switch(requestCode) {
case SELECT_IMAGE:
String imagePath = getPath(data.getData());
break;
}
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
startManagingCursor(cursor);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
getPath() is a function to get the path from the returned URI object. This will return a String with the path you need.
Cheers!

Categories

Resources