How to Transfer an Image from Its Url to the Sd Card

How do I transfer an image from its URL to the SD card?

First you must make sure your application has permission to write to the sdcard. To do this you need to add the uses permission write external storage in your applications manifest file. See Setting Android Permissions

Then you can you can download the URL to a file on the sdcard. A simple way is:

URL url = new URL ("file://some/path/anImage.png");
InputStream input = url.openStream();
try {
//The sdcard directory e.g. '/sdcard' can be used directly, or
//more safely abstracted with getExternalStorageDirectory()
File storagePath = Environment.getExternalStorageDirectory();
OutputStream output = new FileOutputStream (new File(storagePath,"myImage.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();
}

EDIT :
Put permission in manifest

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Download image from server and store it on SD card

Use an async task to download and save images to external storage.

class DownloadFileFromURL extends AsyncTask<String, String, String> {

/**
* Before starting background thread
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
System.out.println("Starting download");
}

/**
* Downloading file in background thread
* */
@Override
protected String doInBackground(String... f_url) {
int count;
try {
String root = Environment.getExternalStorageDirectory().toString();

System.out.println("Downloading");
URL url = new URL(f_url[0]);

URLConnection conection = url.openConnection();
conection.connect();
// getting file length
int lenghtOfFile = conection.getContentLength();

// input stream to read file - with 8k buffer
InputStream input = new BufferedInputStream(url.openStream(), 8192);

// Output stream to write file

OutputStream output = new FileOutputStream(root+"/downloadedfile.jpg");
byte data[] = new byte[1024];

long total = 0;
while ((count = input.read(data)) != -1) {
total += count;

// writing data to file
output.write(data, 0, count);

}

// flushing output
output.flush();

// closing streams
output.close();
input.close();

} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}

return null;
}

/**
* After completing background task
* **/
@Override
protected void onPostExecute(String file_url) {
System.out.println("Downloaded");
}
}

You just call the above class using

    new DownloadFileFromURL().execute(<The url you want to download from>);

You will also need to add below permissions to Manifest file

  <uses-permission android:name="android.permission.INTERNET" />

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Hope it helps

Android - Saving a downloaded image from URL onto SD card

You will need to first create the directories and sub-directories where you want to create the files.
I see that you used the mkdir() method. Try mkdirs(), and it should work.

How to Save Internet Image with url in SD card (Android Kotlin)

Change the parameter type in

override fun onPostExecute(result: Void)

to

override fun onPostExecute(result: Void?).

And as Akhilesh Kumar mentioned here you should also change

class SaveThisImage : AsyncTask<Void, Void, Void>()

to

class SaveThisImage : AsyncTask<Void, Void, Void?>()

This is because the doInBackground in your code returns a nullable type Void?. The return value of doInBackground is passed as an argument to onPostExcecute but your onPostExecute accepts a non null value Void instead of a nullable one Void?.

As a side-note you should use Unit instead of Void when in Kotlin

EDIT

To make your saved image appear the media database needs to be updated. The media database is updated when you restart your device or after some time passes it will show up in the save location. To update the media database immediately you can use the MediaScannerConnection object.

Try passing your saved file to this function after bitmap.compress and pass your myImageFile

/**
* Updates the Pictures gallery to include the newly created file.
* @param fileObj: file path to be scanned so that the new file will appear in the gallery
*/
private fun refreshPhoneGallery(fileObj: File)
{

/* Scan the specified file path so that the new file will appear in gallery */
MediaScannerConnection.scanFile(
yourContext,
arrayOf(fileObj.path),
null,
object : MediaScannerConnection.OnScanCompletedListener
{
override fun onScanCompleted(scannedFilePath: String?, p1: Uri?)
{
// Do whatever
}
}
)
}

How to get a picture from url site and store it to device?

I wrote this code to receive image from an url. And if you check this link it tells you how to save it on sd-card Store Bitmap image to SD Card in Android

private Bitmap getBitmap(String str, Options options) {
// TODO Auto-generated method stub
Bitmap bm = null;
InputStream inStream = null;
try {
inStream = HttpConnection(str);
bm = BitmapFactory.decodeStream(inStream, null, options);
inStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return bm;
}

How can I save an image from a url?

You have to save it in SD card or in your package data, because on runtime you only have access to these. To do that this is a good example

URL url = new URL ("file://some/path/anImage.png");
InputStream input = url.openStream();
try {
//The sdcard directory e.g. '/sdcard' can be used directly, or
//more safely abstracted with getExternalStorageDirectory()
File storagePath = Environment.getExternalStorageDirectory();
OutputStream output = new FileOutputStream (storagePath + "/myImage.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();
}

Source : How do I transfer an image from its URL to the SD card?

Get image from web and save it to phones memory ?

see this complete example give here

http://android-example-code.blogspot.in/p/download-store-and-read-images-from.html



Related Topics



Leave a reply



Submit