【问题标题】:How to get Image thumbnails that are not in the camera folder如何获取不在相机文件夹中的图像缩略图
【发布时间】:2013-10-18 12:39:52
【问题描述】:

我使用 httpurlconnection 下载了一个图像文件,然后将其保存在外部存储文件夹中以备后用

private Bitmap getBitmap(String path) {

    File f = new File(path);
    File tempFile = null;
    Bitmap bitmap = null;
    if (f.exists()) {
        // from SD projects
        if (f.isDirectory()) {

            return null;
        }
        bitmap = decodeFile(f, 0);
        if (bitmap != null)
            return bitmap;
    } else {
        // from web
        try {

            URL imageUrl = new URL(path.toString());

            HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection();
            conn.setConnectTimeout(TIMEOUT);
            conn.setReadTimeout(TIMEOUT);
            conn.setInstanceFollowRedirects(true);
            InputStream is = conn.getInputStream();

            tempFile = fileCache.getTempFile();
            OutputStream os = new FileOutputStream(tempFile);
            copyStream(is, os);
            os.flush();
            os.close();
            is.close();
            // LogUtils.d(TAG, "required size: " + requiredSize);
            bitmap = decodeFile(tempFile, 0);

            return bitmap;
        } catch (Throwable ex) {
            Log.e(TAG, "error decoding bitmap", ex);
            if (ex instanceof OutOfMemoryError) {
                thumbsCache.clear();
            }
        } finally {

        }
    }
    return bitmap;
}

然后使用此代码获取位图文件缩略图:

public static Bitmap getThumbnail(ContentResolver cr, String path, int kind) {
    Cursor ca = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[] { MediaStore.MediaColumns._ID }, MediaStore.MediaColumns.DATA + "=?", new String[] { path }, null);
    try {
        if (ca != null && ca.moveToFirst()) {
            int id = ca.getInt(ca.getColumnIndex(MediaStore.MediaColumns._ID));
            ca.close();
            return MediaStore.Images.Thumbnails.getThumbnail(cr, id, kind, null);
        }
    } catch (Exception e) {
        Log.w(TAG, "exception getting thumbnail: "+path+ " kind : "+kind);
    }

    finally {
        ca.close();
    }
    return null;
}

文件存在,我检查了 查询不为空 但查询是空的 - 没有行。 这是为什么呢?

如何获取下载图片的缩略图?

【问题讨论】:

    标签: android bitmap thumbnails


    【解决方案1】:

    使用BitmapFactory,并在您的BitmapFactory.Options 上加上适当的inSampleSize 来创建缩略图。

    或者,将您当前的代码替换为 Picasso,教它将您的图像调整为合适的缩略图大小,特别是如果您不需要将完整大小的图像用于其他目的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-13
      • 2020-10-27
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-02
      • 2015-01-22
      相关资源
      最近更新 更多