Android : Download Image from url to the device

on Saturday, March 21, 2015


i'm trying to download image from url then load it


here is my code, and i have no idea why its not working


Downloading Code :



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");


Reading Code :



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 comments:

Post a Comment