0

I download an image file using httpurlconnection, and then save it for later use in external storage folder

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;
}

and then getting the bitmap file thumbnail with this code:

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;
}

The file exists, i checked the query isnt null but the query is empty - no rows. why is that?

how can i get thumbnails for downloaded images ?

4

1 回答 1

1

使用,在您BitmapFactory的适当位置创建缩略图。inSampleSizeBitmapFactory.Options

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

于 2013-10-10T16:32:20.907 回答