Android 下载指定url,保存文件,保存文字

 

 

/**
     * 下载APK
     **/
    private void downloadApk() {
//        String apkUrl = "http://192.168.1.1/downloadtest.apk";
        String apkUrl = "http://apk-cdn.zhangxinhulian.com/com.zxhl.weather-guanwang-1.0.6_106_jiagu.apk";
        Uri uri = Uri.parse(apkUrl);
        DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
        DownloadManager.Request request = new DownloadManager.Request(uri);
        // 设置允许使用的网络类型,这里是移动网络和wifi都可以
        request.setAllowedNetworkTypes(request.NETWORK_MOBILE | request.NETWORK_WIFI);
        //设置是否允许漫游
        request.setAllowedOverRoaming(false);
        //设置文件类型
        MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
        String mimeString = mimeTypeMap.getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(apkUrl));
        request.setMimeType(mimeString);
        //在通知栏中显示
        request.setNotificationVisibility(request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        request.setTitle("download...");
        request.setVisibleInDownloadsUi(true);
        //sdcard目录下的download文件夹
//        SDCardUtils.deleteFile(qq_otherList.get(i).getPath());
        String installPath="sdcard/download/"+pathFile+".apk";
        if (fileIsExists(installPath)){
            SDCardUtils.deleteFile(installPath);
        }
        request.setDestinationInExternalPublicDir("/sdcard/download/", pathFile + ".apk");
        // 将下载请求放入队列

        cachedThreadPool.submit(new Runnable() {
            @Override
            public void run() {
                downloadManager.enqueue(request);
            }
        });
    }


    /**
     * 判断文件是否存在
     * */
    public boolean fileIsExists(String strFile) {
        try {
            File f=new File(strFile);
            if(!f.exists()) {
                return false;
            }
        }
        catch (Exception e) {
            return false;
        }
        return true;
    }


    /**
     * 删除文件
     * @param path
     */
    public static void deleteFile(String path){
        File file = new File(path);
        if (file.exists()) {
            file.delete();
        }
    }








 /**
     * 下载类型广告执行下载任务
     */
    private void startDownload() {
        String apkUrl = "http://apk-cdn.zhangxinhulian.com/com.zxhl.weather-guanwang-1.0.6_106_jiagu.apk";
        Uri uri = Uri.parse(click_url);
        DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
        DownloadManager.Request request = new DownloadManager.Request(uri);
        // 设置允许使用的网络类型,这里是移动网络和wifi都可以
        request.setAllowedNetworkTypes(request.NETWORK_MOBILE | request.NETWORK_WIFI);
        //设置是否允许漫游
        request.setAllowedOverRoaming(false);
        //设置文件类型
        MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
        String mimeString = mimeTypeMap.getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(apkUrl));
        request.setMimeType(mimeString);
        //在通知栏中显示
        request.setNotificationVisibility(request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        request.setTitle("download...");
        request.setVisibleInDownloadsUi(true);
        //sdcard目录下的download文件夹
//        SDCardUtils.deleteFile(qq_otherList.get(i).getPath());
        FileUtils.saveFile(context,"ad.apk");
        request.setDestinationInExternalPublicDir("/sdcard/download/", "ad" + ".apk");
        // 将下载请求放入队列

        fixedThreadPool.execute(new Runnable() {
            @Override
            public void run() {
                downloadManager.enqueue(request);
            }
        });

    }



package com.example.myadsdk.utils;

import android.content.Context;
import android.util.Log;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * author : zhangjian
 * time    : 2020/12/11
 * description :
 */
public class FileUtils {

    /**
     * @param content 保存字符串
     */
    public static void saveContent(Context context,String content){
//        /data/data/包名/files
        String save_path=context.getFilesDir()  +"/log.txt";
        try {
            File f = new File(save_path);
            FileWriter fw = new FileWriter(f, true);
            PrintWriter pw = new PrintWriter(fw);
            pw.println(content);
            pw.flush();
            fw.flush();
            pw.close();
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Log.e("TAG", "saveContent写入成功: " );
    }

    /**
     * 保存文件到报名下
     * @param context
     * @param fileName
     */
    public static void saveFile(Context context,String fileName){
        String save_path=context.getFilesDir()  +"";
        File dirFile = new File(save_path);
        File file = new File(fileName);
        if (!dirFile.exists()) {
            dirFile.mkdirs();
            try {
                file.createNewFile();
            } catch (Exception e) {

            }
        }
    }
}

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jian11058

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值