从url下载图像到设备

时间:2015-03-21 16:02:41

标签: android url download imageview

我试图从网址下载图片 然后加载它

这是我的代码,我不知道为什么它不起作用

下载代码:

                    URL url = new URL("http://myurl.com/" + ItemName + ".png");

                    URLConnection conection = url.openConnection();

                    conection.connect();

                    InputStream input = new BufferedInputStream(url.openStream());
                    File fileUri = new File (CategoryActivity.Application.getCacheDir(), ItemName + ".png");
                    OutputStream output = new FileOutputStream(fileUri);

                    byte data[] = new byte[1024];

                    while ((count = input.read(data)) != -1) {
                        output.write(data, 0, count);
                    }

                    output.flush();
                    output.close();
                    input.close();

                    Log.e("","Done Downloading");

阅读代码:

  File imgFile = new File (CategoryActivity.Application.getCacheDir(), ItemName + ".png");


        Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

        ImageView myImage = (ImageView) findViewById(R.id.imageView1);

        myImage.setImageBitmap(myBitmap);

0 个答案:

没有答案