当前位置: 移动技术网 > 移动技术>移动开发>Android > Android实现下载图片,视频,APK功能等功能

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

2018年04月19日  | 移动技术网移动技术  | 我要评论

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();
    }
}

记得要在子线程哦?

    
    
    
    
    

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网