1

我收到一条错误消息,指出位图使用了太多内存。

我知道我应该使用 bitmap.recyle() 但我不知道把它放在哪里,无论我把它放在哪里我都会收到一个错误,说我正在尝试使用回收的位图。

如果有人可以提供帮助,那就太好了。

这是我的相关代码:

public class PictureViewer extends SherlockActivity implements
    android.view.GestureDetector.OnGestureListener {

private ViewFlipper viewFlipper = null;
private GestureDetector gestureDetector = null;
ArrayList<Integer> number = new ArrayList<Integer>();
DownloadBitmap bit = new DownloadBitmap();

int j = 1;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Remove title bar
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.pictureviewer);
    viewFlipper = (ViewFlipper) findViewById(R.id.viewflipper);
    gestureDetector = new GestureDetector(this);

    for (int i = 1; i <= 65; ++i)
        number.add(i);
    Collections.shuffle(number);

    loadImage();
    loadImage();
}

public void loadImage() {

    if (j == 65) { // Change this number to exact ammount of pictures
        j = 1;
    }
    int next = number.get(j);
    j++;

    ImageView image = new ImageView(this);
    Bitmap bitmap = bit.createBitmapFromUrl("http://comedyzone.mobi/img" + next + ".jpg");
    WeakReference<Bitmap> mBitmapReference = new WeakReference<Bitmap>(bitmap);
    image.setImageBitmap(mBitmapReference.get());
    image.setScaleType(ImageView.ScaleType.FIT_XY);
    viewFlipper.addView(image, new LayoutParams(LayoutParams.FILL_PARENT,
            LayoutParams.FILL_PARENT));
}

@Override
public boolean onDown(MotionEvent arg0) {
    // TODO Auto-generated method stub
    return false;
}

@Override
public boolean onFling(MotionEvent arg0, MotionEvent arg1, float arg2,
        float arg3) {
    // TODO Auto-generated method stub
    if (arg0.getX() - arg1.getX() > 120) {

        this.viewFlipper.setInAnimation(AnimationUtils.loadAnimation(this,
                R.anim.push_left_in));
        this.viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,
                R.anim.push_left_out));
        this.viewFlipper.showNext();
        loadImage();
        return true;
    } else if (arg0.getX() - arg1.getX() < -120) {
        this.viewFlipper.setInAnimation(AnimationUtils.loadAnimation(this,
                R.anim.push_right_in));
        this.viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,
                R.anim.push_right_out));
        this.viewFlipper.showPrevious();
        loadImage();
        return true;
    }
    return true;
}

@Override
public void onLongPress(MotionEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2,
        float arg3) {
    // TODO Auto-generated method stub
    return false;
}

@Override
public void onShowPress(MotionEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public boolean onSingleTapUp(MotionEvent arg0) {
    // TODO Auto-generated method stub
    return false;
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    return this.gestureDetector.onTouchEvent(event);
}

private InputStream OpenHttpConnection(String urlString) throws IOException {
    InputStream in = null;
    int response = -1;

    URL url = new URL(urlString);
    URLConnection conn = url.openConnection();

    if (!(conn instanceof HttpURLConnection))
        throw new IOException("Not an HTTP connection");

    try {
        HttpURLConnection httpConn = (HttpURLConnection) conn;
        httpConn.setAllowUserInteraction(false);
        httpConn.setInstanceFollowRedirects(true);
        httpConn.setRequestMethod("GET");
        httpConn.connect();

        response = httpConn.getResponseCode();
        if (response == HttpURLConnection.HTTP_OK) {
            in = httpConn.getInputStream();
        }
    } catch (Exception ex) {
        throw new IOException("Error connecting");
    }
    return in;
}
4

5 回答 5

6

我知道我应该使用 bitmap.recyle()

recycle没有必要打电话。

去年在 Google IO 上,有一个关于这个主题的演讲。

Google I/O 2011:Android 应用程序的内存管理- 您绝对应该观看所有这些内容,值得花时间。

WeakReference为您的Bitmap对象制作一个良好的开端是更好的Bitmap管理。例如:

Bitmap bitmap = DownloadImage("http://comedyzone.mobi/img" + next + ".jpg");
WeakReference<Bitmap> mBitmapReference = new WeakReference<Bitmap>(bitmap);
image.setImageBitmap(mBitmapReference.get());

有效地显示位图

这些是您也应该阅读的 Android 培训课程。

此外,这是我编写的用于从 URL 下载图像的类。您应该考虑使用它来代替您的DownloadImage方法,它更有效。

下载位图

public class DownloadBitmap {

private static String LOG_TAG = DownloadBitmap.class.getName();

/**
 * @param url
 * @return Bitmap image from the interwebs
 */
static Bitmap createBitmapFromUrl(String url) {
    final Bitmap mBitmap = readBitmapFromNetwork(url);
    final WeakReference<Bitmap> mBitmapReference = new WeakReference<Bitmap>(mBitmap);
    if (mBitmapReference.get() != null)
        return mBitmapReference.get();
    return null;
}

/**
 * @param urlString The URL to read the bitmap from.
 * @return A Bitmap image or null if an error occurs.
 */
private static Bitmap readBitmapFromNetwork(String urlString) {
    InputStream mInputStream = null;
    FlushedInputStream mFlushedInputStream = null;
    Bitmap mBitmap = null;
    WeakReference<Bitmap> mBitmapReference = null;
    try {
        final BitmapFactory.Options mOptions = new BitmapFactory.Options();
        mOptions.inPurgeable = true;
        mOptions.inDither = false;
        final URL mUrl = new URL(urlString);
        final URLConnection mConnection = mUrl.openConnection();
        mConnection.connect();
        mInputStream = mConnection.getInputStream();
        mFlushedInputStream = new FlushedInputStream(mInputStream);
        mBitmap = BitmapFactory.decodeStream(mFlushedInputStream, null, mOptions);
        mBitmapReference = new WeakReference<Bitmap>(mBitmap);
    } catch (MalformedURLException e) {
        if (BuildConfig.DEBUG)
            Log.e(LOG_TAG, "Bad image URL", e);
        return null;
    } catch (IOException e) {
        if (BuildConfig.DEBUG)
            Log.e(LOG_TAG, "Could not get remote image", e);
        return null;
    } finally {
        try {
            if (mInputStream != null)
                mInputStream.close();
            if (mFlushedInputStream != null)
                mFlushedInputStream.close();
        } catch (IOException e) {
            if (BuildConfig.DEBUG)
                Log.e(LOG_TAG, "Error closing stream.");
            return null;
        }
    }
    if (mBitmapReference.get() != null)
        return mBitmapReference.get();
    return null;
}

/**
 * An InputStream that skips the exact number of bytes provided, unless it
 * reaches EOF.
 */
static class FlushedInputStream extends FilterInputStream {
    public FlushedInputStream(InputStream inputStream) {
        super(inputStream);
    }

    @Override
    public long skip(long n) throws IOException {
        long totalBytesSkipped = 0L;
        while (totalBytesSkipped < n) {
            long bytesSkipped = in.skip(n - totalBytesSkipped);
            if (bytesSkipped == 0L) {
                int bytes = read();
                if (bytes < 0) {
                    break;
                } else {
                    bytesSkipped = 1;
                }
            }
            totalBytesSkipped += bytesSkipped;
        }
        return totalBytesSkipped;
    }
  }
}
于 2012-07-13T04:37:51.827 回答
1

在使用它之前,您需要减小位图的样本大小。这可以用来这样做。

 private Bitmap decodeFile(File file)
{
    try 
    {
        //********************* decode image size ********************
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(file), null, options);

        // ********************** Find the correct scale value. It should be the power of 2. ********************
        options.inSampleSize = BitmapConverter.calculateInSampleSize(options, 145, 105);
        options.inJustDecodeBounds = false;

        return BitmapFactory.decodeStream(new FileInputStream(file), null, options);
    } 
    catch(FileNotFoundException eFileNotFoundException) 
    {

        return null;
    }
}

然后,如果需要,您可以调用 Bitmap.recycle(),尽管我觉得没有必要。

于 2012-07-13T05:02:20.287 回答
0

ImageLoader
您可以使用 Imageloader 类在后台加载图像。
图像加载器

于 2012-07-13T04:24:35.287 回答
0

bitmap.recycle()您可以在此语句之后image.setImageBitmap(bitmap);编写函数loadImage(),因为在此语句之后不再需要位图。

发生此错误是因为您使用了非常大的位图,或者您的代码中存在内存泄漏。

每当您使用位图时,请始终尝试使用所有可能的选项以尽可能少地使用内存。

有关更多信息,请参阅我对同一问题的回答。

于 2012-07-13T04:33:59.893 回答
0

一般来说,您应该在不再需要时标记要清理的对象。在您的情况下,我看到您不再需要这些位图的唯一一次是当活动不再位于最前面时。

我所看到的是您的 ViewFlipper 可能会使用不必要的大位图。您应该在将它们设置到 ImageView 之前调整位图的大小,而不是在添加它们之后调整 ImageView 的大小。尝试使用 inSampleSize 作为 BitmapFactory 的选项,并在将位图加载到 ImageView 之前使用 Bitmap.createScaledBitmap() 调整位图的大小。

于 2012-07-13T05:35:40.223 回答