无法使用Android中的url.openConnection或Picasso从URL获取图像

时间:2018-05-09 17:56:51

标签: android imageview android-arrayadapter picasso

我正在尝试从ArrayAdapter类中的URL获取图像但不成功。我收到一条错误消息,但它是null。有任何调试建议吗?感谢。

如果尝试使用url.openConnection获取图像

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ...
    ArrayList<String> imageFiles = post.imageFiles;
    for (int i = 0; i < imageFiles.size(); i++) {
        try {
            ImageView imageView = new ImageView(context);
            URL url = new URL(imageFiles.get(i));
            Log.d("DEBUGA1-ImageURL", post.imageFiles.get(i));
            Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
            imageView.setImageBitmap(bmp);
            Log.d("DEBUGA1-Successful: ", "");
        } catch (Exception e) {
            Log.d("DEBUGA1-Error: ", e.getLocalizedMessage() == null ? "error is null message" : e.getLocalizedMessage());
        }
    }
    ...
}

打印

D/DEBUGA1-ImageURL: http://lc-IDX4LztP.cn-n1.lcfile.com/dovltUTrQUMvYA2FlPnLLAD
D/DEBUGA1-Error:: error is null message
D/DEBUGA1-ImageURL: http://lc-IDX4LztP.cn-n1.lcfile.com/KLGH4UDPqfq0PN2rqJhBK5D
D/DEBUGA1-Error:: error is null message
D/DEBUGA1-ImageURL: http://lc-IDX4LztP.cn-n1.lcfile.com/b5eiDFQGYSIf6IYC2xLISZB
D/DEBUGA1-Error:: error is null message
D/DEBUGA1-ImageURL: http://lc-IDX4LztP.cn-n1.lcfile.com/ve00QyIrnuHCkHa0bKbcNAD
D/DEBUGA1-Error:: error is null message
D/DEBUGA1-ImageURL: http://lc-IDX4LztP.cn-n1.lcfile.com/RR2CRPN1DqrhiNjeEBauL8B
D/DEBUGA1-Error:: error is null message

如果尝试使用Picasso获取图像,则onSuccess或onError都没有任何响应。想知道为什么

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ...
    ImageView imageView = new ImageView(context);
    ArrayList<String> imageFiles = post.imageFiles;
    for (int i = 0; i < imageFiles.size(); i++) {
        Picasso.with(context).load(imageFiles.get(i))
                .error(R.mipmap.ic_launcher)
                .fit()
                .centerInside()
                .into(imageView, new com.squareup.picasso.Callback() {
                    @Override
                    public void onSuccess() {
                        Log.d("DEBUGA2-Successful: ", "");
                    }
                    @Override
                    public void onError() {
                        Log.d("DEBUGA2-error", "onError");
                    }
                });
    }
    ...
}

0 个答案:

没有答案