保存网络图片在sdcard两个方法

简介: 保存网络图片在sdcard两个方法

法一

 

Button but = (Button) findViewById(R.id.button1);
        but.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread() {
                    public void run() {
                        try {
                            writeFileData(
                                    "http://cmsimg02.qiniudn.com/contentimg/2014/06/24/20/02c9fe2c0dee6ad4a9d4761e70101452d7.jpg",
                                    "aaa.png");
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    };
                }.start();
            }
        });
    }
    public void writeFileData(String httpUrl, String fileName) {
        File file2 = new File(Environment.getExternalStorageDirectory()
                .getAbsolutePath() + "/yohoboy007/" + fileName);
        file2.getParentFile().mkdirs();
        HttpClient client = new DefaultHttpClient();
        HttpGet get = new HttpGet(httpUrl);
        InputStream inputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            HttpResponse httpResponse = client.execute(get);
            if (httpResponse.getStatusLine().getStatusCode() == 200) {
                inputStream = httpResponse.getEntity().getContent();
                fileOutputStream = new FileOutputStream(file2);
                byte[] buf = new byte[1024 * 5];
                int lengs;
                while ((lengs = inputStream.read(buf)) != -1) {
                    fileOutputStream.write(buf, 0, lengs);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null && fileOutputStream != null) {
                try {
                    inputStream.close();
                    fileOutputStream.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

法二

public void method5(final String url) {
        // String url =
        // "http://cmsimg02.qiniudn.com/contentimg/2014/06/24/20/02c9fe2c0dee6ad4a9d4761e70101452d7.jpg";
        YhHttpUtils.get(url, new BinaryHttpResponseHandler() {
            @Override
            public void onSuccess(byte[] arg0) {
                super.onSuccess(arg0);
                File file = Environment.getExternalStorageDirectory();
                File file2 = new File(file, "yohoboy");
                File file3 = new File(file2, "pic");
                file3.mkdir();
                file3 = new File(file3, getPicName(url));
                try {
                    FileOutputStream oStream = new FileOutputStream(file3);
                    oStream.write(arg0);
                    oStream.flush();
                    oStream.close();
                    // textView.setText("可爱的猫咪已经保存在sdcard里面");
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.i("hck", e.toString());
                }
            }
        });
    }
    public String getPicName(String url) {
        return url.substring(url.lastIndexOf('/') + 1, url.length());
    }
目录
相关文章
|
2月前
|
机器学习/深度学习 算法 调度
14种智能算法优化BP神经网络(14种方法)实现数据预测分类研究(Matlab代码实现)
14种智能算法优化BP神经网络(14种方法)实现数据预测分类研究(Matlab代码实现)
313 0
|
1月前
|
机器学习/深度学习 数据采集 边缘计算
基于灰色神经网络的预测方法
基于灰色神经网络的预测方法
106 0
|
2月前
|
算法 Python
【EI复现】考虑网络动态重构的分布式电源选址定容优化方法(Matlab代码实现)
【EI复现】考虑网络动态重构的分布式电源选址定容优化方法(Matlab代码实现)
|
3月前
|
机器学习/深度学习 数据采集 TensorFlow
基于CNN-GRU-Attention混合神经网络的负荷预测方法(Python代码实现)
基于CNN-GRU-Attention混合神经网络的负荷预测方法(Python代码实现)
135 0
|
4月前
|
存储 Linux 容器
【Container App】在容器中抓取网络包的方法
本文介绍在Azure Container App中安装tcpdump抓取网络包,并通过Storage Account上传抓包文件的方法。内容包括使用curl和nc测试外部接口连通性、长Ping端口、安装tcpdump、抓取网络包、以及通过crul命令上传文件至Azure Storage。适用于需要分析网络请求和排查网络问题的场景。
175 1
|
4月前
|
机器学习/深度学习 边缘计算 算法
基于BP神经网络的电池容量预测方法研究
基于BP神经网络的电池容量预测方法研究
|
6月前
计算网络号的直接方法
子网掩码用于区分IP地址中的网络部分和主机部分,连续的“1”表示网络位,“0”表示主机位。例如,255.255.255.0 的二进制为 11111111.11111111.11111111.00000000,前24位是网络部分。通过子网掩码可提取网络号,如 IP 192.168.1.10 与子网掩码 255.255.255.0 的网络号为 192.168.1.0。此外,文档还介绍了十进制与二进制间的转换方法,帮助理解IP地址的组成与计算。
432 11
|
10月前
|
监控 安全 网络安全
深入解析PDCERF:网络安全应急响应的六阶段方法
PDCERF是网络安全应急响应的六阶段方法,涵盖准备、检测、抑制、根除、恢复和跟进。本文详细解析各阶段目标与操作步骤,并附图例,助读者理解与应用,提升组织应对安全事件的能力。
1470 89
|
8月前
|
缓存 数据中心 网络架构
5个减少网络延迟的简单方法
高速互联网对工作与娱乐至关重要,延迟和断线会严重影响效率和体验。本文探讨了导致连接缓慢的三个关键因素:吞吐量、带宽和延迟,并提供了减少延迟的实用方法。包括重启设备、关闭占用带宽的程序、使用有线连接、优化数据中心位置以及添加内容分发网络 (CDN) 等策略。虽然完全消除延迟不可能,但通过这些方法可显著改善网络性能。
1961 7
|
8月前
|
Kubernetes Shell Windows
【Azure K8S | AKS】在AKS的节点中抓取目标POD的网络包方法分享
在AKS中遇到复杂网络问题时,可通过以下步骤进入特定POD抓取网络包进行分析:1. 使用`kubectl get pods`确认Pod所在Node;2. 通过`kubectl node-shell`登录Node;3. 使用`crictl ps`找到Pod的Container ID;4. 获取PID并使用`nsenter`进入Pod的网络空间;5. 在`/var/tmp`目录下使用`tcpdump`抓包。完成后按Ctrl+C停止抓包。
302 12

热门文章

最新文章