Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
314 views
in Technique[技术] by (71.8m points)

android - Download Images From URL to SD Card

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);

    }

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.with(ImageDetailActivity.this).load(
galleryObjects.get(mViewPager.getCurrentItem()).fullImagePath).into(target);


Picasso Target example

private Target target = new Target() {
    @Override
    public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
        new Thread(new Runnable() {
            @Override
            public void run() {

                File file = new File(
                    Environment.getExternalStorageDirectory().getPath() 
                    + "/saved.jpg");
                try {
                        file.createNewFile();
                        FileOutputStream ostream = new FileOutputStream(file);
                        bitmap.compress(Bitmap.CompressFormat.JPEG,100,ostream);
                        ostream.close();
                }
                catch (Exception e) {
                        e.printStackTrace();
                }
            }
        }).start();
    }

    @Override
    public void onBitmapFailed(Drawable errorDrawable) {}

    @Override
    public void onPrepareLoad(Drawable placeHolderDrawable) {}
};

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...