Если вам необходимо загрузить изображения и сохранить их на SD-карту. Пожалуйста, следуйте приведенному ниже коду:
package com.tablet;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.net.UnknownHostException;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
/*
* This class download & store the media in external storage
* */
public class DownloadActivity extends Activity {
private static String fileName = "android.jgp";
private Button btnDownload;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnDownload = (Button) findViewById(R.id.download);
btnDownload.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// TODO Auto-generated method stub
download();
}
});
private void download()
{
System.out.println("Inside Download");
try {
// this is the file you want to download from the remote server
String path = "http://code.google.com/android/goodies/wallpaper/android-wallpaper5_1024x768.jpg";
URL u = new URL(path);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
String PATH = "/data/Kamal"
//Environment.getExternalStorageDirectory()
// + "/download/";
+ "/";
Log.v("log_tag", "PATH: " + PATH);
File file = new File(PATH);
file.mkdirs();
File outputFile = new File(file, fileName);
FileOutputStream f = new FileOutputStream(outputFile);
InputStream in = c.getInputStream();
Log.v("log_tag"," InputStream consist of : "+in.read());
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = in.read(buffer)) > 0) {
//System.out.println("Reading byte : "+in.read(buffer));
f.write(buffer, 0, len1);
}
Toast.makeText(this, "Download Completed Successfully @ "+PATH, Toast.LENGTH_LONG).show();
f.close();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(this, "MalformedURLException", Toast.LENGTH_LONG).show();
} catch (ProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(this, "ProtocolException", Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(this, "FileNotFoundException", Toast.LENGTH_LONG).show();
}catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(this, "UnknownHostException", Toast.LENGTH_LONG).show();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(this, "IOException", Toast.LENGTH_LONG).show();
}
}
}