Get image description from Uri - android

I m just trying to get image description from image uri
I already got the image (Bitmap) with this uri but I can't get the description of it
Its a pretty simple question. I hope you guys can help me :)
by "image description" I talking about what I wrote with this function: insertImage(ContentResolver cr, String imagePath, String name, String description) (last arguement is what I want to get again (once its saved))

this is how I did:
String[] projection = {MediaStore.Images.Media.DESCRIPTION};
Cursor c = MediaStore.Images.Media.query(getContentResolver(),intent.getData(),projection);
c.moveToFirst();
String description = c.getString(c.getColumnIndex(MediaStore.Images.Media.DESCRIPTION));

Related

Performing Album Art Checks on an Audio file

I've been attempting to look for a solution with regards to the problem as stated in the title above but to no avail.
I'm trying to find a way to check if an Audio file has or does not have an album art, so in the case when there is no album art, I'll be able to set my own drawable for it.
Here's the code I'm running to initialize my song list.
ContentResolver cr = getActivity().getContentResolver();
Uri songsUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String songsSelection = MediaStore.Audio.Media.IS_MUSIC + "!= 0";
String songsSortOrder = MediaStore.Audio.Media.TITLE + " ASC";
Cursor songCur = cr.query(songsUri, null, songsSelection, null, songsSortOrder);
int songCount = 0;
if(songCur != null)
{
songCount = songCur.getCount();
if(songCount > 0)
{
while(songCur.moveToNext())
{
//String data = songCur.getString(songCur.getColumnIndex(MediaStore.Audio.Media.DATA));
// Debug
//Log.e("Song Path", data);
// (long _id, long _albumId, long _artistId, String _title,
// String _artistName, String _albumName, int _duration)
//Log.e("Music ID", songCur.getString(cur.getColumnIndex(MediaStore.Audio.Media._ID)));
//Log.e("Music Album ID", songCur.getString(songCur.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID)));
//Log.e("Music Artist ID", songCur.getString(cur.getColumnIndex(MediaStore.Audio.Media.ARTIST_ID)));
//Log.e("Music Title", songCur.getString(cur.getColumnIndex(MediaStore.Audio.Media.TITLE)));
//Log.e("Music Artist Name", songCur.getString(cur.getColumnIndex(MediaStore.Audio.Media.ARTIST)));
//Log.e("Music Album Name", songCur.getString(songCur.getColumnIndex(MediaStore.Audio.Media.ALBUM)));
//Log.e("Music Duration", songCur.getString(cur.getColumnIndex(MediaStore.Audio.Media.DURATION)));
mediaManager.songFiles.add(new Song(
songCur.getString(songCur.getColumnIndex(MediaStore.Audio.Media.DATA)),
songCur.getLong(songCur.getColumnIndex(MediaStore.Audio.Media._ID)),
songCur.getLong(songCur.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID)),
songCur.getLong(songCur.getColumnIndex(MediaStore.Audio.Media.ARTIST_ID)),
songCur.getString(songCur.getColumnIndex(MediaStore.Audio.Media.TITLE)),
songCur.getString(songCur.getColumnIndex(MediaStore.Audio.Media.ARTIST)),
songCur.getString(songCur.getColumnIndex(MediaStore.Audio.Media.ALBUM)),
songCur.getInt(songCur.getColumnIndex(MediaStore.Audio.Media.DURATION))));
}
}
songCur.close();
}
Let me know if you require any further explanation on my question.
I noticed in your code that you're getting retrieving the album_id of a particular song so a while back I discovered a simple way to fetch album art by appending the album_id to a particular URI, checkout the following code
public Uri getAlbumArtUri(long albumID) {
return ContentUris.withAppendedId(Uri.parse("content://media/external/audio/albumart"), albumID);
}
I then use an Image Loading library like Universal Image Loader or Glide to load the image into an ImageView, like so:
ImageLoader.getInstance().displayImage
(getAlbumArtUri(song.albumId).toString(),
holder.albumArt, new DisplayImageOptions.Builder().cacheInMemory(true).
showImageOnFail(R.drawable.albumart_mp_unknown).resetViewBeforeLoading(true).
build());
showImageOnFail() takes in a drawable that would be displayed incase of error.(This means the song doesn't have album artwork)
P.S I also found the solution discussed on this Stack Overflow Post Quite useful:
Most robust way to fetch Album Art in Android
Hope this helps!

How to get Resource name?

How to get image name and display it in a textbox?
I used arraylist that generates random images.
String ImageName = getResources().getResourceName((Integer)list.get(position));
The output I get from this code is R.drawable.image1. How to get the image name image1?
try this,
String name = getResources().getResourceEntryName(image[position]);
Try this
string.substring(string.lastIndexOf("."),string.length());
String name = "image1";
int imgId = getResources().getIdentifier("drawable/" + name, "drawable", getPackageName());

Android storeimage also creates thumbnail

Android:
I am using following code to insert image into album
According to description this method insert image also creates its thumbnail i don't want thumbnail to be created how can i stop thumbnail creation ?
Problem is I write image in album but when i select image from album it returns the thumbnail image path which is not required.
insert image parameters
Parameters
scr The content resolver to use
source The stream to use for the image
title The name of the image
description The description of the image
MediaStore.Images.Media.insertImage(contentResolver ,file.getAbsolutePath(),
file.getName(), );
I struggled with this as well. To get the actual image data location using the images resource URI:
String dataColumnName = MediaStore.Images.Media.DATA;
Uri mediaUri = //Image's URI;
if (mediaUri != null)
{
String[] proj = { dataColumnName };
Cursor actualimagecursor = managedQuery(mediaUri, proj, null, null, null);
int actual_image_column_index = actualimagecursor.getColumnIndexOrThrow(dataColumnName);
boolean hasValues = actualimagecursor.moveToFirst();
if (hasValues)
{
//this is the file location of the image
String path = actualimagecursor.getString(actual_image_column_index);
}
}

Displaying image from the sqlite database using cursor in android

i want to display image in imageview from sqlite database using cursor. i used below code to retrieve image but i am not able to display image in imageview.
Cursor c=this.db.query(TABLE_NAME, new String[] { "name","price","image" },
null, null, null, null, null);
name.setText(c.getString(0));
price.setText(c.getString(1));
byte b[]=c.getBlob(2);
Bitmap bp=BitmapFactory.decodeByteArray(b, 0, b.length);
ImageView image=(ImageView)findViewById(R.id.ImageView);
//ByteArrayInputStream imageStream = new ByteArrayInputStream(b);
//Bitmap theImage = BitmapFactory.decodeStream(imageStream);
//image.setImageBitmap(theImage);
image.setImageBitmap(bp);
other than image, name and price will display but image will not display.
any suggestion or code that will help me to solve this problem.
Please help me.
Thanks in advance..
I've tried to use Stream instead of byte array, mine example simply works for me.
Also try to use Cursor.getColumnIndex()
byte[] blob = c.getBlob(c.getColumnIndex(YourDB.IMAGE));
ByteArrayInputStream inputStream = new ByteArrayInputStream(blob);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
Here's my "create table" statement, pls double check yours
create table datatable(_id INTEGER PRIMARY KEY AUTOINCREMENT, image BLOB, price INTEGER);
First of all please did not store the images in database because sqlite will not support more then 1 mb,i may be wrong, images should be store in assets or drawble folder and the name of images should be store in database then you can get it from name to resource conversion by code
String videoFileName= c.getString(c.getColumnIndex(TB_SETTING_VIDEOFILENAME));
int dot = videoFileName.lastIndexOf(".");
videoFileName=videoFileName.substring(0,dot);
Log.d("VIDEOFILENAME", videoFileName);
int videoFileRId = getResources().getIdentifier(videoFileName , "raw", getPackageName());
I hope this will help you.
try this
cursor object; // your cursor
mImageView.setImageBitmap(getImageFromBLOB(object.getBlob(object.getColumnIndex("book_thumb"))));
public static Bitmap getImageFromBLOB(byte[] mBlob)
{
byte[] bb = mBlob;
return BitmapFactory.decodeByteArray(bb, 0, bb.length);
}

Get Selected Image File Location in Android

I am currently making an app which works with images. I need to implement functionality where the user picks a file stored on the SD card. Once they pick the picture (using the Android gallery), the the file-location of the image will be sent to another Activity, where other work will be done upon it.
I have seen similar posts here on SO, but none to answer my question specifically. Basically this is the code I am doing when the user clicks the "Load a Picture" button:
// Create a new Intent to open the picture selector:
Intent loadPicture = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// To start it, run the startActivityForResult() method:
startActivityForResult(loadPicture, SELECT_IMAGE);
From that code, I then have a onActivityResult() method to listen to the call-back:
// If the user tried to select an image:
if(requestCode == SELECT_IMAGE)
{
// Check if the user actually selected an image:
if(resultCode == Activity.RESULT_OK)
{
// This gets the URI of the image the user selected:
Uri selectedImage = data.getData();
// Create a new Intent to send to the next Activity:
Intent i = new Intent(currentActivty.this, nextActivity.class);
// ----------------- Problem Area -----------------
// I would like to send the filename to the Intent object, and send it over.
// However, the selectedImage.toString() method will return a
// "content://" string instead of a file location. How do I get a file
// location from that URI object?
i.putExtra("PICTURE_LOCATION", selectedImage.toString());
// Start the activity outlined with the Intent above:
startActivity(i);
As the code above states, the uri.toString() will return a content:// string instead of the file location of the selected picture. How do I obtain the file location?
Note: Another possible solution is to send over the content:// string and convert that into a Bitmap (which is what happens in the next Activity). However, I don't know how to do that.
I have found the answer to my own question. After doing some more searching, I finally stumbled upon a post here on SO which asks the same question here: android get real path by Uri.getPath().
Unfortunately, the answer has a broken link. After some Google searching, I found the correct link to the site here: http://www.androidsnippets.org/snippets/130/ (I have verified that this code does indeed work.)
However, I decided to take a different route. Since my next Activity is using an ImageView to display the picture, I am instead going to use the Uri content string for all methods that link to the next Activity.
In the next Activity, I am using the ImageView.setImageUri() method.
Here is the code I am doing in the next Activity to display the picture from the content:// string:
// Get the content string from the previous Activity:
picLocation = getIntent().getStringExtra("PICTURE_LOCATION");
// Instantiate the ImageView object:
ImageView imageViewer = (ImageView)findViewById(R.id.ImageViewer);
// Convert the Uri string into a usable Uri:
Uri temp = Uri.parse(picLocation);
imageViewer.setImageURI(temp);
I hope that this question and answer will be helpful to future Android developers.
Here's another answer that I hope someone finds useful:
You can do this for any content in the MediaStore. In my app, I have to get the path from URIs and get the URI from paths. The former:
/**
* Gets the corresponding path to a file from the given content:// URI
* #param selectedVideoUri The content:// URI to find the file path from
* #param contentResolver The content resolver to use to perform the query.
* #return the file path as a string
*/
private String getFilePathFromContentUri(Uri selectedVideoUri,
ContentResolver contentResolver) {
String filePath;
String[] filePathColumn = {MediaColumns.DATA};
Cursor cursor = contentResolver.query(selectedVideoUri, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
filePath = cursor.getString(columnIndex);
cursor.close();
return filePath;
}
The latter (which I do for videos, but can also be used for Audio or Files or other types of stored content by substituting MediaStore.Audio (etc) for MediaStore.Video:
/**
* Gets the MediaStore video ID of a given file on external storage
* #param filePath The path (on external storage) of the file to resolve the ID of
* #param contentResolver The content resolver to use to perform the query.
* #return the video ID as a long
*/
private long getVideoIdFromFilePath(String filePath,
ContentResolver contentResolver) {
long videoId;
Log.d(TAG,"Loading file " + filePath);
// This returns us content://media/external/videos/media (or something like that)
// I pass in "external" because that's the MediaStore's name for the external
// storage on my device (the other possibility is "internal")
Uri videosUri = MediaStore.Video.Media.getContentUri("external");
Log.d(TAG,"videosUri = " + videosUri.toString());
String[] projection = {MediaStore.Video.VideoColumns._ID};
// TODO This will break if we have no matching item in the MediaStore.
Cursor cursor = contentResolver.query(videosUri, projection, MediaStore.Video.VideoColumns.DATA + " LIKE ?", new String[] { filePath }, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(projection[0]);
videoId = cursor.getLong(columnIndex);
Log.d(TAG,"Video ID is " + videoId);
cursor.close();
return videoId;
}
Basically, the DATA column of MediaStore (or whichever sub-section of it you're querying) stores the file path, so you use what you know to look up the DATA, or you query on the DATA field to select the content you care about.

Categories

Resources