Android实现下载图片,视频,APK功能等功能

public void downPhotos(String url, String path, String photosName) throws IOException {
    long fileSize;
    File out = new File(path, photosName + ".jpg");
    URL myURL = new URL(url);
    URLConnection conn = myURL.openConnection();
    conn.connect();
    InputStream is = conn.getInputStream();
    fileSize = conn.getContentLength();
    if (fileSize <= 0)
        throw new RuntimeException("can not know the file`s size");
    if (is == null)
        throw new RuntimeException("stream is null");
    FileOutputStream fos = new FileOutputStream(out);
    byte buf[] = new byte[1024];
    do {
        // 循环读取
        int numread = is.read(buf);
        if (numread == -1) {
            break;
        }
        fos.write(buf, 0, numread);
    } while (true);
    try {
        is.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

记得要在子线程哦 

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>


发布了1991 篇原创文章 · 获赞 486 · 访问量 243万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 成长之路 设计师: Amelia_0503

分享到微信朋友圈

×

扫一扫,手机浏览