【问题标题】:Download image from 'download url' Android从“下载网址”Android 下载图片
【发布时间】:2015-01-26 21:31:22
【问题描述】:

有大量来自 url 代码的下载图片。但是是否可以从已经下载的 url 下载图像?我的意思是;

关于从link下载此图像的大量代码

但我想从这个link 下载图像到安卓设备

有可能吗?

谢谢

【问题讨论】:

  • 这是可能的,但您必须分两步/下载完成。如果您以通常的方式使用 http 客户端“下载”该 URL,您将获得一个 html 页面而不是图像。解析该 html 页面以获取图像的 url,然后下载该图像。
  • @greenapps 有那个例子吗?
  • 你不是第一个想要/需要这个的人。事实上,我以前在这个网站上见过它。

标签: android image url download


【解决方案1】:

这没什么难的。

公共类 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 卡中。不要忘记在清单中授予外部存储权限。

【讨论】:

  • 您确定此代码会将图像保存在您的设备上吗?它实际上对我不起作用。我得到文件 I\O 异常
  • 这就是我在最后一行明确提到的。不要忘记声明此权限。 声明了吗?
【解决方案2】:

试试看这个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);

【讨论】:

  • 感谢您的回复,但我想下载第二个网址。这对我没有帮助
  • 在数组中使用,根据位置调用需要显示的url。你可以参考这个。shadowhelios.blogspot.in/2014/09/…@ozi 它已经保存在sd卡中了。
  • @Shadow 示例使用androidexample.com/media/webservice/LazyListView_images/… 这个。但请点击我的第二​​个链接。它直接下载文件没有显示所以我需要保存下载的图像没有显示一个。
  • 您需要将该文件直接下载并保存到sd卡吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-17
  • 1970-01-01
  • 1970-01-01
  • 2016-04-01
  • 1970-01-01
相关资源
最近更新 更多