i am new in android. i want to save image from url into SDcard.
File direct = new File(Environment.getExternalStorageDirectory()
+ "/.imgapp");
if (!direct.exists()) {
direct.mkdirs();
}
Calendar c = Calendar.getInstance();
int d = c.get(Calendar.DATE);
int d1 = c.get(Calendar.MONTH)+1;
File file = new File(Environment.getExternalStorageDirectory()
+ "/.imgapp/"+d+""+d1+".png" );
if (!file.exists()) {
URL url = new URL ("file://some/path/anImage.png");
InputStream input = url.openStream();
try {
OutputStream output = new FileOutputStream (Environment.getExternalStorageDirectory()
+ "/.imgapp/"+d+""+d1+".png");
try {
byte[] buffer = new byte[aReasonableSize];
int bytesRead = 0;
while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) {
output.write(buffer, 0, bytesRead);
}
} finally { output.close(); }
} finally { input.close(); }
}else{
Toast.makeText(getApplicationContext(), "No Error", Toast.LENGTH_SHORT).show();
}
manifest file
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
i try This Code But App Stop and Crash please Help Me. i want download image onCreate when user open App.
finally Done!!.
DownloadManager mgr = (DownloadManager) this.getSystemService(Context.DOWNLOAD_SERVICE);
String uRl = "http://bitsparrow.altervista.org/wp-content/uploads/2013/04/5.jpg";
Uri downloadUri = Uri.parse(uRl);
DownloadManager.Request request = new DownloadManager.Request(
downloadUri);
request.setAllowedNetworkTypes(
DownloadManager.Request.NETWORK_WIFI
| DownloadManager.Request.NETWORK_MOBILE)
.setAllowedOverRoaming(false).setTitle("Demo")
.setDescription("Something useful. No, really.")
.setDestinationInExternalPublicDir("/.appimg", "test.jpg");
mgr.enqueue(request);
This Work For me..!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.