Viewing image from sql table - android

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

Related

BitmapFactory.decodeFile return null, on /storage/emulated/0/DCIM/Camera/IMG_20200407_144901.jpg

I don't know why BitmapFactory.decodeFile("/storage/emulated/0/DCIM/Camera/IMG_20200407_144901.jpg") return null
if I get the absolutle path from a picture of gallery.
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// Result code is RESULT_OK only if the user selects an Image
if (resultCode == MainActivity.RESULT_OK)
switch (requestCode) {
case GALLERY_REQUEST_CODE:
//data.getData return the content URI for the selected Image
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
// Get the cursor
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
// Move to first row
cursor.moveToFirst();
//Get the column index of MediaStore.Images.Media.DATA
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
//Gets the String value in the column
String imgDecodableString = cursor.getString(columnIndex);
cursor.close();
// Set the Image in ImageView after decoding the String
//Picasso.with(this).load(imgDecodableString).fit().into(imageView);
//imageView.setImageURI(data.getData());
BitmapFactory.Options options = new BitmapFactory.Options ();
options.inSampleSize = 4;
imageView.setImageBitmap(BitmapFactory.decodeFile(selectedImage.getPath()));
break;
/* case CAMERA_REQUEST_CODE:
imageView.setImageURI(Uri.parse(cameraFilePath));
break;*/
default:
super.onActivityResult(requestCode, resultCode, data);
}
}

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?

Not able to select few photos from Gallery in Android

I'am invoking default gallery app from my app to select any photo. Below is my code to get the selected image path from gallery. It's working fine for all the photos except few. When i select any of PICASA uploaded photos from Gallery, app is force closing. Please help me.
Inside onActivityResult()....
Uri selectedImage = data.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 selectedPhotoPath = cursor.getString(columnIndex).trim(); <<--- NullPointerException here
cursor.close();
bitmap = BitmapFactory.decodeFile(selectedPhotoPath);
......
Sometimes data.getData(); returns null depending on the app you use to get the picture. A workaround for this is to use the above code in onActivityResult:
/**
*Retrieves the path of the image that was chosen from the intent of getting photos from the galery
*/
Uri selectedImageUri = data.getData();
// OI FILE Manager
String filemanagerstring = selectedImageUri.getPath();
// MEDIA GALLERY
String filename = getImagePath(selectedImageUri);
String chosenPath;
if (filename != null) {
chosenPath = filename;
} else {
chosenPath = filemanagerstring;
}
The variable chosenPath will have the correct path of the chosen image. The method getImagePath() is this:
public String getImagePath(Uri uri) {
String selectedImagePath;
// 1:MEDIA GALLERY --- query from MediaStore.Images.Media.DATA
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();
selectedImagePath = cursor.getString(column_index);
} else {
selectedImagePath = null;
}
if (selectedImagePath == null) {
// 2:OI FILE Manager --- call method: uri.getPath()
selectedImagePath = uri.getPath();
}
return selectedImagePath;
}
Please try below code
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK){
Uri targetUri = data.getData();
textTargetUri.setText(targetUri.toString());
Bitmap bitmap;
try {
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(targetUri));
....
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
OR
Please check below link
OR
How to pick an image from gallery (SD Card) for my app?
String ImagePath = "";
private void setImageFromGallery(Intent data) {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = this.getContentResolver().query(selectedImage, filePathColumn, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
Log.i("choosepath", "image" + picturePath);
ImagePath = picturePath;
} else {
ImagePath = selectedImage.getPath(); // Add this line
}
   ImageView imgView = (ImageView) findViewById(R.id.imgView);
   imgView.setImageBitmap(BitmapFactory.decodeFile(imagePath));
Bitmap bitmap = Utilities.rotateImage(pictureImagePath);
}
Current Android system (first version -> 2.3.3 -> even 4.4.2) looks like not able to selected multiple files, so you need custom gallery to do that.
Afer researched so many times, I found Custom Camera Gallery library can help you do that thing already.

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.

Categories

Resources