Tuesday, May 22, 2012

how to write image content in file and read from file and display in imageview

<<<<<<<<<<< write image content in directory>>>>>>>>>>>>>>


private boolean writeToImage() {


try {
URL url = new URL (imgUrl);
InputStream input = url.openStream();
File outputFile=new File(Directory,"thumb.jpg");
String ImageFile=outputFile.toString();
   OutputStream output = new FileOutputStream (ImageFile);
   try {
       byte[] buffer = new byte[1024];
       int bytesRead = 0;
       while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) {
           output.write(buffer, 0, bytesRead);
       }
   } finally {
       output.close();
   }
} catch (Exception e) {
MRLog.e("PreSetupProductDownload : writeToImage :: Exception = " + e.toString());
}

return true;
}

<<<<<<<<<<>>>>>>>>>>>>>>>>>>>


                 File imageFile = new File(Directory,"thumb.jpg");

if(imageFile.exists()){

try {
FileInputStream fis = new FileInputStream(imageFile);
Drawable d=Drawable.createFromStream(fis, "src");
if(d!=null){
Image.setImageDrawable(d);
} else {
Log.d("error in read image");

}

} catch (Exception e) {
e.printStackTrace();
}
}

No comments:

Post a Comment