漂泊的河 2016-02-26 03:09 采纳率: 33.3%
浏览 1393
已采纳

安卓关于下载图片和下载文件的问题

图片

安卓中下载图片有几种方法?用URL下载?

文件

文件都是从服务器中下载下来后解析吗?

  • 写回答

3条回答

  • Exploring1024 2016-02-26 03:37
    关注

    如果自己加载的话,需要自己写网络去请求url地址进行下载。类似于这样

     URL url = new URL(uri);
                    HttpURLConnection conn = (HttpURLConnection)url.openConnection();
                    conn.setRequestMethod("GET");
                    conn.setReadTimeout(10000);
    
                    if (conn.getResponseCode() == 200) {
                        InputStream fis =  conn.getInputStream();
                        ByteArrayOutputStream bos = new ByteArrayOutputStream();
                        byte[] bytes = new byte[1024];
                        int length = -1;
                        while ((length = fis.read(bytes)) != -1) {
                            bos.write(bytes, 0, length);
                        }
                        picByte = bos.toByteArray();
                        bos.close();
                        fis.close();
    
                        Message message = new Message();
                        message.what = 1;
                        handle.sendMessage(message);
                    }
    
    
                }catch (IOException e) {
                    e.printStackTrace();
                }
    

    第二种就是用开源的一些框架,比如ImageLoader:

     public class ImageLoaderPicture {
    
        private DisplayImageOptions options;
    
        public ImageLoaderPicture(Context context) {
    
            ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context).threadPriority(Thread.NORM_PRIORITY - 2)
            .denyCacheImageMultipleSizesInMemory()
            .discCacheFileNameGenerator(new Md5FileNameGenerator())
            .tasksProcessingOrder(QueueProcessingType.LIFO).enableLogging() 
            .memoryCache(new WeakMemoryCache())                                 
            .build();
            ImageLoader.getInstance().init(config);
    
            options = new DisplayImageOptions.Builder()
            .showStubImage(0)
            .showImageForEmptyUri(0)
            .showImageOnFail(0)
            .cacheInMemory().cacheOnDisc()
            .imageScaleType(ImageScaleType.IN_SAMPLE_INT)
            .bitmapConfig(android.graphics.Bitmap.Config.RGB_565)
            .build();
        }
    
        public DisplayImageOptions getOptions() {
            return options;
        }
    
        public void setOptions(DisplayImageOptions options) {
            this.options = options;
        }
    
    

    最后还可以使用谷歌官方提供的框架Volley进行。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用