从'下载网址下载图片' Android的

时间:2014-11-28 08:56:38

标签: android image url download downloading

网址代码中有大量的下载图片。但是可以从已经动作下载的URL下载图像吗?我的意思是;

有关从link

下载此图片的大量代码

但我想从此link

将图像下载到Android设备

有可能吗?

由于

2 个答案:

答案 0 :(得分:0)

尝试查看此tutorial,对我非常有帮助。他使用名为ImageLoader的自定义类和公共方法DisplayImage(String url, int loader, ImageView imageView),您可以这样调用它:

int loader = R.drawable.loader;

// Imageview to show
ImageView image = (ImageView) findViewById(R.id.image);

// Image url
String image_url = "http://www.example.com/images/sample.jpg";

// ImageLoader class instance
ImageLoader imgLoader = new ImageLoader(getApplicationContext());

imgLoader.DisplayImage(image_url, loader, image);

答案 1 :(得分:0)

这没什么难的。

公共类DownloadImage扩展了Activity {

String image_URL=http://www.hdwallpapers.in/download/minions_2015-1280x720.jpg; // give image   name to save in sd card

String extStorageDirectory;
String sdCard;
Bitmap bitmap;
File file;
String savedFilePath;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button buttonSave = (Button)findViewById(R.id.save);

    ImageView bmImage = (ImageView)findViewById(R.id.image);
    buttonSave.setOnClickListener(new OnClickListener()
    {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            myAsyncTask myWebFetch = new myAsyncTask();
            myWebFetch.execute();
        }
    });
}

class myAsyncTask extends AsyncTask<Void, Void, Void>    {
    TextView tv;

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        dialog.dismiss();

    }
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
          public ProgressDialog dialog;
        dialog = new ProgressDialog(DownloadImage.this);
        dialog.setMessage("Loading...");
        dialog.setCancelable(false);
        dialog.setIndeterminate(true);
        dialog.show();
    }
    protected Void doInBackground(Void... arg0) {
        try {
            //set the download URL, a url that points to a file on the internet
            //this is the file to be downloaded
            URL url = new URL(image_URL);

            //create the new connection
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

            //set up some things on the connection
            urlConnection.setRequestMethod("GET");
            urlConnection.setDoOutput(true);

            //and connect!
            urlConnection.connect();

            //set the path where we want to save the file
            //in this case, going to save it on the root directory of the
            //sd card.
            File SDCardRoot = Environment.getExternalStorageDirectory();
            //create a new file, specifying the path, and the filename
            //which we want to save the file as.
            File file = new File(SDCardRoot,"somefile.jpg");

            //this will be used to write the downloaded data into the file we created
            FileOutputStream fileOutput = new FileOutputStream(file);

            //this will be used in reading the data from the internet
            InputStream inputStream = urlConnection.getInputStream();

            //this is the total size of the file
            int totalSize = urlConnection.getContentLength();
            //variable to store total downloaded bytes
            int downloadedSize = 0;

            //create a buffer...
            byte[] buffer = new byte[1024];
            int bufferLength = 0; //used to store a temporary size of the buffer

            //now, read through the input buffer and write the contents to the file
            while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
                //add the data in the buffer to the file in the file output stream (the file on the sd card
                fileOutput.write(buffer, 0, bufferLength);
                //add up the size so we know how much is downloaded
                downloadedSize += bufferLength;
                //this is where you would do something to report the prgress, like this maybe
                //updateProgress(downloadedSize, totalSize);

            }
            //close the output stream when done
            fileOutput.close();

        //catch some possible errors...
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }  
}

}

现在您直接在SD卡中下载了图像。不要忘记在外部存储的清单中给予许可。