【问题标题】:download images from FTP :Caused by: java.lang.OutOfMemoryError从 FTP 下载图像:引起:java.lang.OutOfMemoryError
【发布时间】:2013-11-29 08:24:24
【问题描述】:
at com.example.newpingziyi.stir.CheckSdcard$LoadImagesFromSDCard.doInBackground(CheckSdcard.java:316)

错误行是更强!

First.show 类似 java.lang.OutOfMemoryError 的错误! 这是代码...

class LoadImagesFromSDCard extends AsyncTask<Object, LoadedImage, Object> {
@Override
protected Object doInBackground(Object... params) {

    Bitmap newBitmap = null;
    File file = new File(localPath);
    String[] filepath = file.list();
    for (String str : filepath) {
        String filename = str;
        String imagePath = localPath + "/" + filename;
        File files = new File(imagePath);
        FileInputStream is = null;
        BufferedInputStream bis = null;
        try {
            is = new FileInputStream(new File(imagePath));
            bis = new BufferedInputStream(is);

            //this line was wrong!
            Bitmap bitmap = BitmapFactory.decodeStream(bis);//this lines was wrong!!


            is.close();
            bis.close();
            if (bitmap != null) {
                newBitmap = Bitmap.createScaledBitmap(bitmap, 70, 70,
                        true);
                bitmap.recycle();
                if (newBitmap != null) {
                    publishProgress(new LoadedImage(newBitmap));
                }

            }
        } catch (IOException e) {
        }
    }
    return null;
}

@Override
public void onProgressUpdate(LoadedImage... value) {
    addImage(value);
}

@Override
protected void onPostExecute(Object result) {
    imageAdapter.notifyDataSetChanged();
}

}

Bitmap bitmap = BitmapFactory.decodeStream(bis);//这行写错了!!

现在我在 code.still OutOfMemoryError 下方进行更改!

class LoadImagesFromSDCard extends AsyncTask<Object, LoadedImage, Object> {
    @Override
    protected Object doInBackground(Object... params) {

        Bitmap newBitmap = null;
        File file = new File(localPath);
        String[] filepath = file.list();
        for (String str : filepath) {
            String filename = str;
            String imagePath = localPath + "/" + filename;
            File files = new File(imagePath);
            FileInputStream is = null;
            BufferedInputStream bis = null;
            try {
                is = new FileInputStream(new File(imagePath));
                bis = new BufferedInputStream(is);
                bis.mark(0);
                BitmapFactory.Options opts = new BitmapFactory.Options();
                opts.inJustDecodeBounds = true;
                BitmapFactory.decodeStream(bis, null, opts);
                int sizes = (opts.outWidth * opts.outHeight);
                if (sizes > 1024 * 1024 * 4) {
                    int zoomRate = 2;
                    if (zoomRate <= 0)
                        zoomRate = 1;
                    opts.inSampleSize = zoomRate;
                }
                opts.inJustDecodeBounds = false;
                bis.reset();


                //this line was wrong!
                Bitmap bitmap = BitmapFactory.decodeStream(bis, null, opts);//this lines was wrong!!


                is.close();
                bis.close();
                if (bitmap != null) {
                    newBitmap = Bitmap.createScaledBitmap(bitmap, 70, 70,
                            true);
                    bitmap.recycle();
                    if (newBitmap != null) {
                        publishProgress(new LoadedImage(newBitmap));
                    }

                }
            } catch (IOException e) {
            }
        }
        return null;
    }

    @Override
    public void onProgressUpdate(LoadedImage... value) {
        addImage(value);
    }

    @Override
    protected void onPostExecute(Object result) {
        imageAdapter.notifyDataSetChanged();
    }
}

Bitmap bitmap = BitmapFactory.decodeStream(bis, null, opts);//这行!

【问题讨论】:

  • 那么位图有多大?
  • 可能1.4MB,显示应用程序停止运行对话框,但应用程序不完全自动!Logcat java.lang.RuntimeException: An error occured while executing doInBackground()``Caused by: java.lang.OutOfMemoryError
  • 这个链接会帮助你:stackoverflow.com/questions/477572/…
  • 像素 计有多大?一个 1.4MB 的图像可能会像内存中的位图一样大。
  • 是的,使用更大的图像证明正在运行,

标签: java android bitmap ftp out-of-memory


【解决方案1】:

这是我下载位图的工作代码,也许会有所帮助:

    private Bitmap downloadBitmap(String url) {
    // Getting the url from the html
    url = url.substring(url.indexOf("src=\"") + 5, url.length() - 1);
    url = url.substring(0, url.indexOf("\""));

    final DefaultHttpClient client = new DefaultHttpClient();

    final HttpGet getRequest = new HttpGet(url);
    try {
        HttpResponse response = client.execute(getRequest);

        //check 200 OK for success
        final int statusCode = response.getStatusLine().getStatusCode();

        if (statusCode != HttpStatus.SC_OK) {
            Log.w("ImageDownloader", "Error " + statusCode +
                    " while retrieving bitmap from " + url);
            return null;
        }
        final HttpEntity entity = response.getEntity();
        if (entity != null) {
            InputStream inputStream = null;
            try {
                // getting contents from the stream
                inputStream = entity.getContent();

                // decoding stream data back into image Bitmap that android understands
                final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                return bitmap;
            } finally {
                if (inputStream != null) {
                    inputStream.close();
                }
                entity.consumeContent();
            }
        }
    } catch (Exception e) {
        // You Could provide a more explicit error message for IOException
        getRequest.abort();
        Log.e("ImageDownloader", "Something went wrong while" +
                " retrieving bitmap from " + url + e.toString());
    }
    return null;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多