如何从网上下载图像并将其保存在内部存储中?

时间:2013-02-23 15:47:26

标签: android

如何从网上下载图像并将其保存在内部存储中?之后,我该如何显示它? 任何帮助将不胜感激

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Bitmap bitmap = getBitmap("http://www.parkcinema.az/uploads/structures/movies/images/vozvra%C3%BCenie_geroya_poster1_resized.jpg");
        ImageView image = (ImageView)findViewById(R.id.pick);
        image.setImageBitmap(bitmap);
    }

public Bitmap getBitmap(String bitmapUrl){
        try {
            URL url = new URL(bitmapUrl);
            return BitmapFactory.decodeStream(url.openConnection().getInputStream());
            }
        catch(Exception ex) {return null;}
        }

1 个答案:

答案 0 :(得分:0)

 private DownloadImageTask task;   

public void onCreate(){
task = new DownloadImageTask();

}   

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {


        protected Bitmap doInBackground(String... urls) {
            String urldisplay = urls[0];
            Bitmap mIcon11 = null;
            try {
                InputStream in = new java.net.URL(urldisplay).openStream();
                mIcon11 = BitmapFactory.decodeStream(in);
            } catch (Exception e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return mIcon11;
        }

        protected void onPostExecute(Bitmap result) {
            // Do your staff here to save image
        }
    }