I have 2 Buttons in My activity. 1st One: Upload Image 2nd One: Get Uploaded image name. Now i have Pressed "Upload Image" Button and selected a image from gallery and that image is displayed in ImageView. Then i Pressed 2nd Button "Get Uploaded Image Name" and a Toast will display the name of that image which is in ImageView. I want the code of this only.
Uri uri = data.getData();
String[] projection = { MediaStore.Images.Media._ID, MediaStore.Images.Media.BUCKET_ID,
MediaStore.Images.Media.BUCKET_DISPLAY_NAME };
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
ArrayList<String> ids = new ArrayList<String>();
mAlbumsList.clear();
if (cursor != null) {
while (cursor.moveToNext()) {
Album album = new Album();
int columnIndex = cursor.getColumnIndex(MediaStore.Images.Media.BUCKET_ID);
album.id = cursor.getString(columnIndex);
if (!ids.contains(album.id)) {
columnIndex = cursor.getColumnIndex(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
album.name = cursor.getString(columnIndex);
columnIndex = cursor.getColumnIndex(MediaStore.Images.Media._ID);
album.coverID = cursor.getLong(columnIndex);
mAlbumsList.add(album);
ids.add(album.id);
} else {
mAlbumsList.get(ids.indexOf(album.id)).count++;
}
}
cursor.close();
This is the way that you can retrieve the image name.. :
Uri selectedImage = data.getData();
String selectedImageName = selectedImage.getLastPathSegment();
Related
I am ( a beginner android) trying to get the real path (ex: image_name.jpg etc) of Image selected from the gallery by the user.
Here is the code snippet:
if (requestCode == SELECT_FILE) {
Uri selectedImage = data.getData();
Bitmap bitmapImage;
try {
bitmapImage =decodeBitmap(selectedImage );
photoImage = (ImageView) findViewById(R.id.photoImage);
photoImage.setImageBitmap(bitmapImage);
photoName = (TextView) findViewById(R.id.designPhoto);
File thePath = new File(getRealPathFromURI(selectedImage));
photoName.setText(getRealPathFromURI(selectedImage));
} catch (Exception e) {
e.printStackTrace();
}
}
private String getRealPathFromURI(Uri contentURI) {
String thePath = "no-path-found";
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(contentURI, filePathColumn, null, null, null);
if(cursor.moveToFirst()){
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
thePath = cursor.getString(columnIndex);
}
cursor.close();
return thePath;
}
but I am not getting anything in the photoName textView also tried lots of codes and guides but with no sucess.
but when I tried it with
photoName.setText(selectedImage.getPath());
I am getting
/document/image:n (where n=any numbers) ex:/document/image:147
but I want a proper file name or path ex: image_name.jpg or /path/image_name.png
trying it from last 3 hours, it would be great if anyone could help me out. humble Thanks in advance.
I got it working using
MediaStore.Images.Media.DISPLAY_NAME
updated and working code might help others:
private String getRealPathFromURI(Uri contentURI) {
String thePath = "no-path-found";
String[] filePathColumn = {MediaStore.Images.Media.DISPLAY_NAME};
Cursor cursor = getContentResolver().query(contentURI, filePathColumn, null, null, null);
if(cursor.moveToFirst()){
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
thePath = cursor.getString(columnIndex);
}
cursor.close();
return thePath;
}
How to change the shape of an uploaded image (Image from photo gallery) into circular form in android eclipse.
This is how you use it :
//this the button which allows you to access the gallery
pic.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
gallery();
}
});
//this allows you select one image from gallery
public void gallery() {
intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), 1);
}
//when you start activity for result and choose an image, the code will automatically continue here
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == 1) {
Uri current_ImageURI = data.getData();
selectedImagePath = getPath(current_ImageURI);
image.setImageDrawable(RoundedBitmapDrawableFactory.create(getResources(),selectedImagePath));
}
}
}
public String getPath(Uri contentUri) {
// we have to check for sdk version because from lollipop the retrieval of path is different
if (Build.VERSION.SDK_INT < 21) {
// can post image
String[] proj = {MediaStore.Images.Media.DATA};
Cursor cursor = getActivity().getApplicationContext().getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} else {
String filePath = "";
String wholeID = DocumentsContract.getDocumentId(contentUri);
// Split at colon, use second item in the array
String id = wholeID.split(":")[1];
String[] column = {MediaStore.Images.Media.DATA};
// where id is equal to
String sel = MediaStore.Images.Media._ID + "=?";
Cursor cursor = getActivity().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;
}
}
Now the two functions below :
decode the sample size of the bitmap (to do correct scaling) and the other extracts the bitmap from the path are also no longer to be used
The Circular Bitmap class is no longer to be used
For better understanding please use this link fr reference : http://developer.android.com/reference/android/support/v4/graphics/drawable/RoundedBitmapDrawableFactory.html
I've made an image gallery activity that is needed for my app. All works well except that it shows an image album for every music album I have in my SD card that contains album art. The default gallery app manages to hide these albums-pictures. How can I achieve the same ?
I get image albums with this code:
Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
String[] projection = { MediaStore.Images.Media._ID, MediaStore.Images.Media.BUCKET_ID,
MediaStore.Images.Media.BUCKET_DISPLAY_NAME,MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
ArrayList<String> ids = new ArrayList<String>();
if (cursor != null) {
while (cursor.moveToNext()) {
PhotoAlbum album = new PhotoAlbum();
int columnIndex = cursor.getColumnIndex(MediaStore.Images.Media.BUCKET_ID);
album.id = cursor.getString(columnIndex);
if (!ids.contains(album.id)) {
//get album name
columnIndex = cursor.getColumnIndex(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
album.name = cursor.getString(columnIndex);
//get image id for the album cover
columnIndex = cursor.getColumnIndex(MediaStore.Images.Media._ID);
album.coverID = cursor.getInt(columnIndex);
//get filename of album cover image
columnIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
album.fileName=new File(cursor.getString(columnIndex)).getParentFile().getAbsolutePath();
album.count++; albumList.add(album); ids.add(album.id); album.bm=null;
} else { albumList.get(ids.indexOf(album.id)).count++; }
}
cursor.close();
}
Hi I am developing an Android Gallery app where I am fetching images from built in gallery and displaying it.I am using the code as below
String[] projection = {MediaStore.Images.Thumbnails._ID};
Cursor cursor = getContentResolver().query(MediaStore.Images.Thumbnails.INTERNAL_CONTENT_URI,
projection, // Which columns to return
null, // Return all rows
null,
null);
int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
int size = cursor.getCount();
// If size is 0, there are no images on the SD Card.
if (size == 0)
{
Log.e("size 0","0");
}
The Problem is when I run this code on Phones having only internal storage (Galaxy Nexus) I am getting Log which says size as Zero even though there are images in built in gallery. How do I resolve this.
Please Help.Thanks!
To get list of Gallery images you can try this
String[] projection = new String[]{
MediaStore.Images.Media._ID,
MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
MediaStore.Images.Media.DATE_TAKEN
};
Uri imageUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
Cursor cur = getContentResolver().query(imageUri,
projection, // Which columns to return
null, // Which rows to return (all rows)
null, // Selection arguments (none)
null // Ordering
);
Log.i("Images Count"," count="+cur.getCount());
Try this on your button like "Browse"
browse.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
and you cal also set selected image into your ImageView as
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
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]);
picturePath = cursor.getString(columnIndex);
cursor.close();
imageView = (ImageView) findViewById(R.id.property_image);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
In first block of code i use startActivityForResult(i, RESULT_LOAD_IMAGE); this return result to called activity and we can get this result by second block of code, and set selected image in your ImageView
String[] projection = {MediaStore.Images.Media._ID};
Replace MediaStore.Images.Thumbnails.INTERNAL_CONTENT_URI with MediaStore.Images.MEDIA.EXTERNAL_CONTENT_URI
Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection, // Which columns to return
null, // Return all rows
null,
null);
int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);
int size = cursor.getCount();
// If size is 0, there are no images on the SD Card.
if (size == 0)
{
Log.e("size 0","0");
}
EDIT: Get the list of thumbnails and get the image URI from the cursor
Uri uri=MediaStore.Images.Thumbnails.getContentUri("external");
Cursor cursor=MediaStore.Images.Thumbnails.queryMiniThumbnails
(getContentResolver(), uri, MediaStore.Images.Thumbnails.MINI_KIND,null);
if( cursor != null && cursor.getCount() > 0 ) {
String uri = cursor.getString( cursor.getColumnIndex( MediaStore.Images.Thumbnails.DATA ) );
}
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.