I am trying to create a very simple Image Downloading app. in which i want to download all images from this url to sd card: https://www.dropbox.com/sh/5be3kgehyg8uzh2/AAA-jYcy_21nLBwnZQ3TBFAea
this code works to load image in imageview:
package com.example.imagedownloadsample;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.ImageView;
import com.squareup.picasso.Picasso;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.image_download);
final ImageView img = (ImageView) (findViewById(R.id.imageView1));
// File file = new File(Environment.getExternalStorageDirectory(),
// "Android/data/com.usd.pop");
Picasso.with(getApplicationContext())
.load("http://8020.photos.jpgmag.com/3456318_294166_528c960558_m.jpg")
.into(img);
}
}
but when i tried like this to download image to sd card i end-up with unfortunatelly app stopped error:
package com.example.imagedownloadsample;
import java.io.File;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.ActionBarActivity;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Target;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.image_download);
//final ImageView img = (ImageView) (findViewById(R.id.imageView1));
File file = new File(Environment.getExternalStorageDirectory(),
"Android/data/com.usd.pop");
Picasso.with(getApplicationContext())
.load("http://8020.photos.jpgmag.com/3456318_294166_528c960558_m.jpg")
.into((Target) file);
}
}
Check these links
Why use Android Picasso library to download images?
http://www.opensourcealternative.org/tutorials/android-tutorials/android-picasso-save-image-tutorial/
http://www.101apps.co.za/articles/gridview-tutorial-using-the-picasso-library.html
http://www.youtube.com/watch?v=tRTFwzUH_ek
http://square.github.io/picasso/
Using Picasso, your code will be of only 1 line
To store into external sd card
Android saving file to external storage
Write a file in external storage in Android
http://www.javatpoint.com/android-external-storage-example
Using picasso to save images in sd card, you can do something like following
Here "Target" is a class provided by picasso, and it has very simple method to understand...
Hope this will meet your needs
Aquery is the Fastest and Easiest way to download image from url to sdcard.
For more reference click here http://androiddhina.blogspot.in/2015/03/androiddownload-image-using-aquery.html
You can use this
AsyncTask
, so that you don't getOnMainThread
exception.Copy this class into your activity. It mustn't be in another method.
And you can call this like
new DownloadFile().execute(“yoururl”);
Also, add these permissions to your manifest.
Use Picasso and load into a Target
I agree with Ichigo Kurosaki's answer above. Here is a detailed example of how you can use Picasso and a Picasso Target.
How you call the Picasso code
Picasso Target example