Android的获得从缩略图路径的形象? [英] Android getting path to image from Thumbnail?
问题描述
我想目前出货的关键更新我的应用程序在市场上。我需要查询的MediaStore缩略图,以及缩略图加载到一个GridView。到目前为止好,现在我只需要得到只在我有什么(这是路径缩略图),根据用户的外部存储器的实际fullsized图片的路径。我需要能够执行每当用户点击缩略图4的动作,分享,查看,移动和删除......但我不能只用缩略图路径做这些,我都试过了,不工作:/。下面是我实现这个片段下方,任何帮助或指导将大大AP preciated!
I'm trying to ship a pivotal update to my app currently on the market. I need to query the thumbnails in the MediaStore, and load the the thumbnails into a GridView. So far so good, now I just need to get the path of the actual fullsized image on the user's External Storage based only on what I have(which is the path to the thumbnail). I need to be able to perform 4 actions whenever a user clicks a thumbnail, Share, View, Move and Delete...but I can't do any of these with just the thumbnail path, I've tried everything, doesn't work :/. Here's a snippet of my implementation below, any help or guidance on this would be greatly appreciated!
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
int columnIndex = 0;
String[] projection = {MediaStore.Images.Media.DATA};
Cursor cursor = managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, projection, null, null, null);
if(cursor != null){
columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToPosition(position);
imagePath = cursor.getString(columnIndex);
FileInputStream is = null;
BufferedInputStream bis = null;
try{
is = new FileInputStream(new File(imagePath));
bis = new BufferedInputStream(is);
Bitmap bitmap = BitmapFactory.decodeStream(bis);
useThisBitmap = Bitmap.createScaledBitmap(bitmap, parent.getWidth(), parent.getHeight(), true);
bitmap.recycle();
}catch(Exception e){
//Try to recover
}
finally{
try{
if(bis != null){
bis.close();
}
if(is != null){
is.close();
}
cursor.close();
projection = null;
}catch(Exception e){
}
}
}
Toast.makeText(this, " " + imagePath, Toast.LENGTH_SHORT).show();
到目前为止,我使用的ImagePath成员用尽了一切办法,但无论是导致抛出异常或其他错误。有什么建议?
So far I've tried everything using the imagePath member, but that either causes exceptions to be thrown or other errors. Any suggestions?
推荐答案
我已经找到了解决办法。我查询MediaStore内容Resolver和抓缩略图(我需要的不是反正)之前。这种优化可以让我搜索MediaStore以同样的确切方式,但这次我在寻找的实际图像,而且我返回缩略图MICRO_KIND。的图像,然后在位图数组缓存,然后用于填充在GridView。我现在能够得到的路径的实际,全尺寸图像上的device..in情况下,我需要使用该路径来创建一个URI实例或文件,现在我可以很容易。对于任何人有这个或类似的问题,<一个href=\"http://stackoverflow.com/questions/9279553/android-getting-path-to-image-from-thumbnail\">Android从缩略图获取路径形象?,这里是下面的解决方案code。顺便说一句,我在AsyncTask的这样做的,显示/驳回加载对话框已显示设备的所有影像后。
I've found the solution. Before I was querying the MediaStore Content Resolver and grabbing Thumbnails(not what I needed anyways). This optimization allows me to search the MediaStore in the same exact way, but this time I'm searching for the ACTUAL Images, and I'm returning the Thumbnail MICRO_KIND. The images are then cached in an array of Bitmaps and then used to populate the GridView. I am now able to get the path to the actual, full size image on the device..in case I need to create a URI instance or a File using that path, now I can easily. For anyone else having this or a similar issue Android getting path to image from Thumbnail?, here's the solution code below. Btw, I'm doing this in an AsyncTask and showing/dismissing a loading dialog after all images on device have been displayed.
/*----------------------------ASYNC TASK TO LOAD THE PHOTOS--------------------------------------------------------*/
public class LoadPhotos extends AsyncTask<Object, Object, Object>{
@Override
protected Object doInBackground(Object... params) {
final String[] columns = { MediaStore.Images.Media._ID };
final String orderBy = MediaStore.Images.Media._ID;
Cursor imagecursor = managedQuery(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,
null, orderBy);
int image_column_index = imagecursor
.getColumnIndex(MediaStore.Images.Media._ID);
AllPhotosActivity.count = imagecursor.getCount();
AllPhotosActivity.windows = new Bitmap[AllPhotosActivity.count];
for (int i = 0; i < AllPhotosActivity.count; i++) {
imagecursor.moveToPosition(i);
//i = index;
int id = imagecursor.getInt(image_column_index);
windows[i] = MediaStore.Images.Thumbnails.getThumbnail(
getApplicationContext().getContentResolver(), id,
MediaStore.Images.Thumbnails.MICRO_KIND, null);
}
imagecursor.close();
return null;
}
@Override
protected void onPostExecute(Object result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
pd.dismiss();
imagegrid.setAdapter(new ImageAdapter(getApplicationContext()));
}
@Override
protected void onProgressUpdate(Object... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
}
}
这是我的onItemClick()
在文件名成员,你会用什么来创造任何你需要的做形象的URI或实例。
And here's my onItemClick() The "filename" member is what you would use to create instances of URIs or whatever you need to do with the image.
public void onItemClick(AdapterView<?> arg0, android.view.View v,
int position, long id) {
String[] columns = { MediaStore.Images.Media.DATA,
MediaStore.Images.Media._ID };
Cursor actualimagecursor = managedQuery(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns,
null, null, null);
final int dataColumnIndex = actualimagecursor
.getColumnIndex(MediaStore.Images.Media.DATA);
final int idColumIndex = actualimagecursor
.getColumnIndex(MediaStore.Images.Media._ID);
actualimagecursor.moveToPosition(position);
filename = actualimagecursor.getString(dataColumnIndex);
final long imageId = actualimagecursor.getLong(idColumIndex);
这篇关于Android的获得从缩略图路径的形象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!