美文网首页
Android极速开发之SD卡缓存文件

Android极速开发之SD卡缓存文件

作者: Javen205 | 来源:发表于2016-08-27 22:39 被阅读180次
  • 读取某个文件夹中的所有Apk文件路径并打开安装页面
  • 读取某文件夹下的所有apk文件
  • 获取SD卡跟目录中的某个文件
  • 弹出安装界面
  • 卸载apk
/**
 * 
 * @author Javen
 *
 */
public class SDKUtil {
    private final static String TAG=SDKUtil.class.getSimpleName();
    
    
    /**
     * 读取某个文件夹中的所有Apk文件路径并打开安装页面
     * @param context
     * @param path
     */
     public static void readApkAndStart(Context context,String path){
            List<String> listpath = readAllApkForPath(context, path);
            if (listpath!=null && listpath.size()>0) {
                for (String string : listpath) {
                    SDKUtil.openInstallView(context, string);
                }
            }else {
                L.e("xxxx-----", "readApkAndStart null。。。。。。。");
            }
     }
     /**
     * 读取某文件夹下的所有apk文件
     * @param context
     * @param path
     * @return
     */
    public static List<String> readAllApkForPath(Context context,String path){
        List<String> fileNameList=new ArrayList<String>();
        File dir;
        if (Parameter.isDebug) {
             dir = getSDir(context, path);
        }else {
             dir = getDiskCacheDir(context, path);
        }
        
        if (dir.isDirectory()) {
            File[] files = dir.listFiles();
            for (File file : files) {
                String filePath = file.getAbsolutePath();
                if (filePath.endsWith(".apk")) {
                    fileNameList.add(filePath);
                }
            }
            return fileNameList;
        }
        return null;
    }
    
    
    /**
     * 获取SD卡跟目录中的某个文件
     * @param context
     * @param uniqueName
     * @return
     */
    public static File getSDir(Context context, String uniqueName) {
        boolean externalStorageAvailable = Environment
                .getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
        String path = null;
        if (externalStorageAvailable) {
            path=Environment.getExternalStorageDirectory().getAbsolutePath();
        }
        return new File(path + File.separator + uniqueName);
    }
    
    /**
     * 获取缓存地址
     * @param context
     * @param uniqueName
     * @return
     */
    public static File getDiskCacheDir(Context context, String uniqueName) {
        boolean externalStorageAvailable = Environment
                .getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
        String cachePath;
        if (externalStorageAvailable) {
            cachePath = context.getExternalCacheDir().getPath();
           
        } else {
            cachePath = context.getCacheDir().getPath();
        }
        L.i(TAG, cachePath);
        return new File(cachePath + File.separator + uniqueName);
    }
    
    /**
     * 弹出安装界面
     * @param context 
     * @param pathString apk 路径
     */
    public static void openInstallView(Context context,String pathString){
        //启动安装界面
//      Intent install = new Intent(Intent.ACTION_VIEW);
//      install.setDataAndType(Uri.fromFile(new File(pathString)),
//              "application/vnd.android.package-archive");
//      install.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//      context.startActivity(install);
        ApkController.install(pathString, context);
    }
    /**
     * 卸载apk
     * @param context
     * @param packageName
     */
    public static void uninstallApk(Context context, String packageName) {  
        Uri uri = Uri.parse("package:" + packageName);  
        Intent intent = new Intent(Intent.ACTION_DELETE, uri);  
        context.startActivity(intent);  
    }  
}

相关文章

  • Android极速开发之SD卡缓存文件

    读取某个文件夹中的所有Apk文件路径并打开安装页面 读取某文件夹下的所有apk文件 获取SD卡跟目录中的某个文件 ...

  • RxCache--打造自己的Android缓存框架

    简介 提供Android缓存功能,包括对SD卡,内存、Sharedpreference以及同时存储SD卡和内存的双...

  • RxCache实现Android缓存框架

    简介 提供Android缓存功能,包括对SD卡,内存、Sharedpreference以及同时存储SD卡和内存的双...

  • android sd卡缓存

    1、通过url下载内容到sd卡中的download的文件夹里面,并把路径加入到cache的缓存里。2、删除你下载的...

  • Android Bitmap 缓存策略

    文章参考:简书博客 图片的缓存分为内存缓存和sd卡缓存。也可以配合使用。 Android 中图片缓存遵循的策略是:...

  • Adnroid文件存储路径getFilesDir()与getEx

    作为一个开发者,我们经常需要通过缓存一些文件到SD卡中,常见的方式就是,通过: File sdCard = Env...

  • Android文件File

    一、SD的使用SD卡文件权限 sdk卡的读写权限 二、SD的使用SD卡挂载状态判断 三、SD的使用SD卡根目录文件...

  • 运行时权限

    自从6.0开始,SD卡的读写也需要获取运行时权限。我们应尽量避免去动态获取SD卡权限,而是将文件缓存在Androi...

  • Android10文件读写适配

    android10,读写sd卡文件时报错,java.io.FileNotFoundException: /stor...

  • android 本地存储

    Android 本地存储分为内存存储和sd卡存储,都是采用文件的方式进行存储,内存指的是手机运行时内存,sd卡则通...

网友评论

      本文标题:Android极速开发之SD卡缓存文件

      本文链接:https://www.haomeiwen.com/subject/uyetettx.html