从url中获取图像并将其存储在本地,然后在listview中使用这些图像

时间:2014-09-09 17:46:17

标签: android

我想从我的设备上保存网址中的图像,然后使用存储在设备上的图像来填充列表视图,还想知道在手机内存或缓存中将图像存储在设备上的位置

1 个答案:

答案 0 :(得分:0)

new ImageDownloader().execute(downloadUrl);


private class ImageDownloader extends AsyncTask {

    @Override
    protected String doInBackground(String... param) {
        // TODO Auto-generated method stub
        return downloadBitmap(param[0]);
    }

    @Override
    protected void onPreExecute() {
        Log.i("Async-Example", "onPreExecute Called");
    }

    @Override
    protected void onPostExecute(String result) {
        Log.i("Async-Example", "onPostExecute Called");
        Bitmap myBitmap = BitmapFactory.decodeFile(result));
        ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);
        myImage.setImageBitmap(myBitmap);
        simpleWaitDialog.dismiss();

    }

    private String downloadBitmap(String url) {
        // initilize the default HTTP client object
        final DefaultHttpClient client = new DefaultHttpClient();

        //forming a HttoGet request 
        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 SaveBitmapToDir(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;
    }

    private String SaveBitmapToDir(Bitmap bmp)
    {
        FileOutputStream out = null;
        File file = null;
        try {
            SimpleDateFormat s = new SimpleDateFormat("ddMMyyyy_hhmmss");
            String dateTime = s.format(new Date());
            String path = Environment.getExternalStorageDirectory().toString();
            OutputStream fOut = null;
            file = new File(path, "MyApp_"+dateTime+".jpg");
            out = new FileOutputStream(file);
            bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
                return file.getAbsolutePath();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}