The principle is to download the image data from the Internet, while turning the data into drawable and loading to the specified ImageView
Save it as a download_image.jpg and open the picture with intent when clicked ImageView
I write the process of processing a picture as a class
Package com.example.downloadandopenimage;
Import Java.io.File;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.OutputStream;
Import java.net.HttpURLConnection;
Import Java.net.URL;
Import Android.content.Context;
Import android.content.Intent;
Import android.graphics.drawable.Drawable;
Import Android.net.Uri;
Import Android.os.AsyncTask;
Import android.os.Environment;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.ImageView;
Import Android.widget.ProgressBar;
public class Loadandsaveimage {
ImageView view;
Context Mcontext;
Public Loadandsaveimage () {
TODO auto-generated Constructor stub
}
Public Loadandsaveimage (context, String str, ImageView view) {
TODO auto-generated Constructor stub
This.view = view;
Mcontext = context;
New Load_and_save_image (). Execute (STR);
}
void Release () {
File File = new file ("file://"
+ environment.getexternalstoragedirectory (). ToString ()
+ "/download_image.jpg");
if (file.exists ()) {
Boolean deleted = File.delete ();
}
}
Class Load_and_save_image extends Asynctask<string, Void, void> {
@Override
Protected Void doinbackground (String ... surl) {
TODO auto-generated Method Stub
InputStream input = null;
OutputStream output = null;
HttpURLConnection connection = null;
try {
URL url = new URL (surl[0]);
Connection = (httpurlconnection) url.openconnection ();
Connection.connect ();
Download the file
input = Connection.getinputstream ();
File Checker = new file ("file://"
+ environment.getexternalstoragedirectory (). ToString ()
+ "/download_image.jpg");
if (checker.exists ()) {
Boolean deleted = Checker.delete ();
}
Output = new FileOutputStream (Environment
. getExternalStorageDirectory (). ToString ()
+ "/download_image.jpg");
byte data[] = new byte[4096];
int count;
while ((count = input.read (data))!=-1) {
Output.write (data, 0, count);
}
catch (Exception e) {
finally {
try {
if (input!= null)
Input.close ();
if (output!= null) {
Output.close ();
}
catch (IOException ignored) {
}
if (connection!= null)
Connection.disconnect ();
}
return null;
}
protected void OnPostExecute (void result) {
drawable d = drawable.createfrompath (Environment
. getExternalStorageDirectory (). ToString ()
+ "/download_image.jpg");
View.setimagedrawable (d);
View.setonclicklistener (New Onclicklistener () {
@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
Intent Intent = new Intent ();
Intent.setaction (Intent.action_view);
Intent.addflags (Intent.flag_activity_new_task);
Intent.setdataandtype (Uri.parse ("file://")
+ environment.getexternalstoragedirectory ()
. toString () + "/download_image.jpg"),
"image/*");
Mcontext.startactivity (Intent);
}
});
};
}
}
Just write this in the activity:
Image = (ImageView) Findviewbyid (r.id.image);
New Loadandsaveimage (Getapplicationcontext (), "http://toolongdidntread.com/wp-content/uploads/2011/06/upload.png ", image);
Three parameters are content, the URL of the picture, you want to load the ImageView
BTW, because the stored jpg file name is fixed here, please use random number or name when loading multiple pictures.