新的文件(路径)返回null

时间:2019-05-09 14:24:09

标签: java android file storage external

我尝试从外部存储安装apk。但是File方法返回null。

这是我的代码:

Activity activity = RunnerActivity.CurrentActivity;
String dir = "storage/emulated/0/Subscript.apk";
String apid = String.valueOf(BuildConfig.APPLICATION_ID + ".provider");
File outputFile = null;
try {
outputFile = new File(dir);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        Uri apkUri = FileProvider.getUriForFile(activity, apid , outputFile);
        Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
        intent.setData(apkUri);
        intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        activity.startActivity(intent);
    } else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N){
        Uri apkUri = Uri.fromFile(outputFile);
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        activity.startActivity(intent);
    }else {
        Log.i("yoyo","File not found");
    }
} catch (Exception e) {
    Log.i("yoyo",e.toString());//this return, that outputFile is null
}

返回异常:

  

java.lang.NullPointerException:尝试在null上调用虚拟方法'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager,java.lang.String)'对象参考   05-09 18:31:53.361 28484 28519我的溜溜球:在android.support.v4.content.FileProvider.parsePathStrategy(FileProvider.java:560)   05-09 18:31:53.361 28484 28519我的溜溜球:在android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:534)   05-09 18:31:53.361 28484 28519我溜溜球:在android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:376)   05-09 18:31:53.361 28484 28519 I yoyo:在com.seciunda.merka.CustomTabsExt.APKinstall(CustomTabsExt.java:95)   05-09 18:31:53.361 28484 28519我的溜溜球:at java.lang.reflect.Method.invoke(本机方法)   05-09 18:31:53.361 28484 28519我yoyo:在com.yoyogames.runner.RunnerJNILib.CallExtensionFunction(RunnerJNILib.java:1200)   05-09 18:31:53.361 28484 28519 I yoyo:在com.yoyogames.runner.RunnerJNILib.Process(本机方法)   05-09 18:31:53.361 28484 28519我的溜溜球:在com.seciunda.merka.DemoRenderer.onDrawFrame(DemoRenderer.java:686)   05-09 18:31:53.361 28484 28519我的溜溜球:在android.opengl.GLSurfaceView $ GLThread.guardedRun(GLSurfaceView.java:1609)   05-09 18:31:53.361 28484 28519我的溜溜球:在android.opengl.GLSurfaceView $ GLThread.run(GLSurfaceView.java:1293)

请帮助我编写一个代码,可以一键安装sdcard或外部存储设备中的apk

0 个答案:

没有答案