1

基本上我的图像路径看起来像这样:/mnt/sdcard/Pictures/image.jpg 我需要以最快的方式从中获取缩略图的路径。

我正在尝试使用 MediaStore.Images.Thumbnails.queryMiniThumbnail,但无论我通过什么,我都会得到空光标。谢谢!

编辑: 这是带来所有图像路径和缩略图路径并将它们存储在字符串中的函数。我需要的是一个返回特定图像路径(/mnt/sdcard/Pictures/image.jpg)的缩略图路径的函数。谢谢

public String getThumbPaths(ThumbContext ctx) {
    Uri uri = MediaStore.Images.Thumbnails.getContentUri("external");

    Cursor cursor = MediaStore.Images.Thumbnails.queryMiniThumbnails(ctx
            .getActivity().getContentResolver(), uri,
            MediaStore.Images.Thumbnails.MINI_KIND, null);

    int columnIndex = cursor.getColumnIndex(Thumbnails.IMAGE_ID);

    String[] filePathColumn = { MediaStore.Images.Media.DATA };

    StringBuilder stringBuilder = new StringBuilder();

    String id = MediaStore.Images.Media._ID + "=?";

    String orientation="1";

    for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {

        String imageId = cursor.getString(columnIndex);

        Cursor images = ctx.getActivity().managedQuery(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                filePathColumn, id,
                new String[] { imageId }, null);

        String filePath = "";
        if (images != null && images.moveToFirst()) {
            filePath = images.getString(images
                    .getColumnIndex(filePathColumn[0]));
        }

        ExifInterface exifReader;
        try {
            exifReader = new ExifInterface(filePath);
            orientation=exifReader.getAttribute(ExifInterface.TAG_ORIENTATION);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            //e.printStackTrace();
        }


        stringBuilder.append(cursor.getString(1) + ";");
        stringBuilder.append(filePath + ";");
        stringBuilder.append(orientation + ";");
        orientation="1";

    }
    //cursor.close();
    return stringBuilder.toString();
}
4

2 回答 2

1

如果我理解正确,您的图像不在媒体数据库中。您可以使用

ThumbnailUtils.extractTumbnail()

它只需要一个位图。

于 2012-11-27T11:28:19.017 回答
-1

试试看。它将帮助您将图像的缩略图设置为 ImageView。

 im=(ImageView)findViewById(R.id.imageView1);

    byte[] imageData = null;

    try 
    {

    final int THUMBNAIL_SIZE = 64;
    //InputStream is=getAssets().open("apple-android-battle.jpg");
    FileInputStream fis = new FileInputStream("/sdcard/apple.jpg");
    Bitmap imageBitmap = BitmapFactory.decodeStream(fis);

    Float width = new Float(imageBitmap.getWidth());
    Float height = new Float(imageBitmap.getHeight());
    Float ratio = width/height;
    imageBitmap = Bitmap.createScaledBitmap(imageBitmap, (int)(THUMBNAIL_SIZE * ratio), THUMBNAIL_SIZE, false);

    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
    imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    imageData = baos.toByteArray();
    im.setImageBitmap(imageBitmap);
    }
    catch(Exception ex) {

    }

希望它会帮助你。

于 2012-11-27T11:26:57.557 回答