-1

我正在显示来自assets/image 文件夹的图像,但此代码不起作用。此代码显示画廊中资产文件夹中的图像。我正在使用画廊预定义库或 jar 文件。

请专家检查一下。感谢你

AssetManager assetManager = getAssets();
    String[] files = null;
    try {
        files = assetManager.list("image");
    } catch (IOException e) {
        Log.e("tag", e.getMessage());
    }

    for(String filename : files) {
        System.out.println("File name => "+filename);
        InputStream in = null;
        try {
            ImageViewTouch imageView = new ImageViewTouch(Rahul.this); 
            imageView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
          final Options options = new Options();
            options.outHeight = (int) scaleHeight; 
            options.outWidth = (int) scaleWidth;   
            options.inScaled = true;
            options.inPurgeable = true;
            options.inSampleSize = 2;
           in = assetManager.open("image/"+filename); 
           Bitmap bit=BitmapFactory.decodeStream(in);

          imageView.setImageBitmap(bit);

              } catch(Exception e) {
            Log.e("tag", e.getMessage());
        }
    }
    gallery.setAdapter(arrayAdapter);
4

2 回答 2

1

嘿,请检查我对同一问题的回答:位图大小超出 Vm 预算错误 android

在处理这样的位图时,也总是尝试使用最大选项:

 final Options options = new Options();
    options.outHeight = (int) scaleHeight; // new smaller height
    options.outWidth = (int) scaleWidth;   // new smaller width
    options.inScaled = true;
    options.inPurgeable = true;

    // to scale the image to 1/8
    options.inSampleSize = 8;
    bitmap = BitmapFactory.decodeFile(imagePath, options);

这可能会解决您的问题。

于 2012-10-30T15:16:23.270 回答
1

bitmap.recycle();1)在为图像设置新位图之前尝试使用释放内存

BitmapDrawable drawable = (BitmapDrawable) myImage.getDrawable();
Bitmap bitmap = drawable.getBitmap();
if (bitmap != null)
{
    bitmap.recycle();
}

2)如果您的图像太大,请缩小它们:

public static Bitmap decodeFile(File file, int requiredSize) {
        try {

            // Decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(new FileInputStream(file), null, o);

            // The new size we want to scale to

            // Find the correct scale value. It should be the power of 2.
            int width_tmp = o.outWidth, height_tmp = o.outHeight;
            int scale = 1;
            while (true) {
                if (width_tmp / 2 < requiredSize
                        || height_tmp / 2 < requiredSize)
                    break;
                width_tmp /= 2;
                height_tmp /= 2;
                scale *= 2;
            }

            // Decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;

            Bitmap bmp = BitmapFactory.decodeStream(new FileInputStream(file),
                    null, o2);

            return bmp;

        } catch (FileNotFoundException e) {
        } finally {
        }
        return null;
    }

更新

像这样的东西:

for(int i=0; i<it.size();i++) { 
    ImageViewTouch imageView = new ImageViewTouch(GalleryTouchTestActivity.this); 
    imageView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
    Options options = new Options(); 
    options.inSampleSize = 2;
    String photoURL = it.get(i);

    BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
    Bitmap bitmap = drawable.getBitmap();
    if (bitmap != null)
    {
       bitmap.recycle();
    }

    bitmap = BitmapFactory.decodeFile(photoURL);

    imageView.setImageBitmap(bitmap); 
    arrayAdapter.add(imageView);
}
于 2012-10-30T14:56:26.887 回答