从FTP下载图像:引起:java.lang.OutOfMemoryError

时间:2013-11-29 08:24:25

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

at com.example.newpingziyi.stir.CheckSdcard$LoadImagesFromSDCard.doInBackground(CheckSdcard.java:316)

错误行是更强!

首先显示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();
}

}

位图位图= 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 个答案:

答案 0 :(得分:1)

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

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