Android : Downloading an image into internal storage and reading it in android

on Wednesday, July 30, 2014


I am trying to download an image from url and i want to display it in an imageview.My code is as follows:



package com.example.imageinternalstorage;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {
Button btnstoreinternal,btnloadfrominternal;
ImageView iv_category;
boolean download_success;
String URL_image="http://footballultimate.com/storelocator/resource/uploads/";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnstoreinternal=(Button) findViewById(R.id.btnstoreinternal);
btnloadfrominternal=(Button) findViewById(R.id.btnloadfrominternal);
iv_category=(ImageView) findViewById(R.id.iv_category);
btnstoreinternal.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
download_success=downloadFile(URL_image+"Hospital.png");
if(download_success==true)
{
Toast.makeText(getApplicationContext(),"Download success",Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getApplicationContext(),"Download failed",Toast.LENGTH_LONG).show();
}

}
});
btnloadfrominternal.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
iv_category.setImageBitmap(getImageBitmap(MainActivity.this,"Hospital.png"));

}
});
}

public boolean downloadFile(final String path)
{
try
{
URL url = new URL(path);

URLConnection ucon = url.openConnection();
ucon.setReadTimeout(5000);
ucon.setConnectTimeout(10000);

InputStream is = ucon.getInputStream();
BufferedInputStream inStream = new BufferedInputStream(is, 1024 * 5);

File file = new File(MainActivity.this.getDir("filesdir", Context.MODE_PRIVATE) + "/Hospital.png");

if (file.exists())
{
file.delete();
}
file.createNewFile();

FileOutputStream outStream = new FileOutputStream(file);
byte[] buff = new byte[5 * 1024];

int len;
while ((len = inStream.read(buff)) != -1)
{
outStream.write(buff, 0, len);
}

outStream.flush();
outStream.close();
inStream.close();

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

return true;
}

public Bitmap getImageBitmap(Context context,String name){
try{
FileInputStream fis = context.openFileInput(name);
Bitmap b = BitmapFactory.decodeStream(fis);
fis.close();
return b;
}
catch(Exception e){
}
return null;
}
}


When i click the first button the download is shown as successfull.But when i click the second button to display the image in the imageview nothing happens.So can anyone give me a replacement code for the retrival code from memory:



public Bitmap getImageBitmap(Context context,String name){
try{
FileInputStream fis = context.openFileInput(name);
Bitmap b = BitmapFactory.decodeStream(fis);
fis.close();
return b;
}
catch(Exception e){
}
return null;
}


The code for dowloading is:



public boolean downloadFile(final String path)
{
try
{
URL url = new URL(path);

URLConnection ucon = url.openConnection();
ucon.setReadTimeout(5000);
ucon.setConnectTimeout(10000);

InputStream is = ucon.getInputStream();
BufferedInputStream inStream = new BufferedInputStream(is, 1024 * 5);

File file = new File(MainActivity.this.getDir("filesdir", Context.MODE_PRIVATE) + "/Hospital.png");

if (file.exists())
{
file.delete();
}
file.createNewFile();

FileOutputStream outStream = new FileOutputStream(file);
byte[] buff = new byte[5 * 1024];

int len;
while ((len = inStream.read(buff)) != -1)
{
outStream.write(buff, 0, len);
}

outStream.flush();
outStream.close();
inStream.close();

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

return true;
}

0 comments:

Post a Comment