【问题标题】:Android -- How to get images from httpsAndroid -- 如何从 https 获取图片
【发布时间】:2018-03-12 19:15:41
【问题描述】:

我正在尝试使用 AsyncTask 从 https URL 下载位图数组中的图像

这是我的 AsyncTask 代码:

class GetImages extends AsyncTask<String,Void,Bitmap>{
        int position;
        public GetImages(int i){
            position=i;
        }
        @Override
        protected void onPreExecute() {    }

        @Override
        protected Bitmap doInBackground(String... strings) {
            Log.i(TAG,"Enter in background!!!!!!!!!!!!!!!!!!");
            String src=strings[0];
            Bitmap bitmap=null;
            InputStream in;
            int responseCode=-1;
            try {
                URL url = new URL(src);

                HttpsURLConnection httpURLConnection = (HttpsURLConnection) url.openConnection();
                httpURLConnection.setDoOutput(true);
                httpURLConnection.connect();
                responseCode = httpURLConnection.getResponseCode();
                if (responseCode == HttpURLConnection.HTTP_OK) {
                    in = httpURLConnection.getInputStream();
                    bitmap = BitmapFactory.decodeStream(in);
                    in.close();
                }
                Log.i(TAG,"OK!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
            } catch (MalformedURLException e) {
                Log.i(TAG,"!!!!!!!!!Errore1");
                e.printStackTrace();
            } catch (IOException e) {
                Log.i(TAG,"!!!!!!!!!Errore2");
                e.printStackTrace();
            }
            return bitmap;
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            Log.i(TAG,"Saving!!!!!!!!!!!!!!!!!!!!!!!");
            image[position]=bitmap;
        }

    }

其中 image[] 是一个位图数组

然后我执行我的 AsyncTask:

for(i=0;i<Array.length;i++){
...
new GetImages(i).execute(MyHttpsURL[i]);
}

我想要做的是从我的 https url 中保存图像并将其保存在位图数组中以在 RecyclerView 中重用它。问题是我的代码似乎没有下载并将图像放入数组中,因为当我在回收站视图中使用 image[] 时,应该显示下载图像的 ImageViews 是空的。 我的错误是什么?如何将 https 中的图像保存在位图数组中?

【问题讨论】:

  • 数组中有什么? lenght 应该是 length
  • 1) 你有任何错误吗? 2)首先下载您的文件吗?
  • 我建议您使用外部库来执行此操作。到目前为止我用过的最好的是:Volley 和 Picasso
  • 不,我没有收到任何错误,应用程序没有崩溃,图像根本没有下载
  • 我推荐 Glide github.com/bumptech/glide 它来自 google,这是最好的

标签: android bitmap android-asynctask android-recyclerview android-networking


【解决方案1】:

您可以使用像 Picasso 这样的图像加载器库并保存其位图值

Get Bitmap from ImageView loaded with Picasso

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-19
    • 2014-10-17
    相关资源
    最近更新 更多