Real File Path Not Appearing - android

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

Related

Viewing image from sql table

hi iam currently working on an android app that stores images to database and it retrieves it back for image view.I convert the image to bit amp and upload it to mysql table
my code is
following code to fetch image from ImageGallery:
Intent i = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, ACTIVITY_SELECT_IMAGE);
It will start ImageGallery, now you can select an image, and in onActivityResult you can decode the image into bitmap, as explained in the link: here:
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);
}
}
}
the php code to upload bitmap
to server :
$base= $_REQUEST['yourselectedimage'];
$buffer = mysql_real_escape_string($base);
then inserting the $buffer to table blob column type
but i don't know how to display the image bit map from table to an image view,please help me in that...
In your OnActivityresult method, add this code sample.
ImageView imageView = (ImageView) findViewById(R.id.imgView);
imageView.setImageBitmap(BitmapFactory.decodeFile(filePath));
Method would look like ...
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();
ImageView imageView = (ImageView) findViewById(R.id.imgView);
imageView.setImageBitmap(BitmapFactory.decodeFile(filePath));
Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
}

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.

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?

Selected Image Not Returned From MediaStore

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.

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