0

我寻找一种有效调整图像大小的方法,避免出现“OutOfMemory”。为此,我尝试了这种方法: http: //developer.android.com/training/displaying-bitmaps/index.html 要使用这些方法,我必须有一个可绘制标识符,所以我创建了一个这样的可绘制对象:

Drawable image = Drawable.createFromPath(pathName);

现在我不知道如何获取可绘制标识符。该方法getIdentifier()意味着具有可绘制名称,但我没有。

4

2 回答 2

0

感谢您的回答。我终于选择了这种方法来调整图片大小:

private void resizeBitmap(String path) {
        Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        int sampleSize = 0;
        boolean done = false;
        Bitmap source = null;
        while (!done) {
            options.inJustDecodeBounds = false;
            sampleSize++;
            try {
                options.inSampleSize = sampleSize;
                source = BitmapFactory.decodeFile(path, options);
                done = true;
            } catch (OutOfMemoryError e) {

            }
            if (sampleSize > 20) {
                // Exit to avoid infinite loop
                done = true;
            }
        }
        if (source != null) {
            try {
                FileOutputStream out = new FileOutputStream(path);
                source.compress(Bitmap.CompressFormat.JPEG, 90, out);
                out.close();
            } catch (Exception e) {
                Log.e("RESIZE PICTURE", "Unable to resize picture after download : "+e.getMessage());
            }
        }
    }
于 2013-09-27T10:14:54.413 回答
0

要使用这些方法,我必须有一个可绘制标识符,所以我创建了一个这样的可绘制对象:

可绘制图像 = Drawable.createFromPath(pathName);

首先,您不需要创建可绘制对象。其次,如果您的可绘制文件夹中没有图像,那么您不应该使用

BitmapFactory.decodeResource(res, resId, options)

BitmapFactory 还有其他方法如

BitmapFactory.decodeFile (String filePath, options); //it takes your image filepath 
(in case your image is in Sdcard) as a string.
or,
BitmapFactory.decodeStream (InputStream, options); // if the image is placed in your asset folder instead of drawables.
于 2013-09-27T08:33:05.990 回答