繁体   English   中英

以编程方式安装apk会导致解析错误

[英]Installing apk programatically causing parse error

我正在尝试以编程方式安装apk。

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/AppFolder/" + "app.apk")), "application/vnd.android.package-archive");
startActivity(intent);

这是我使用的代码。 但是,当我运行代码时,它提示选择软件包安装程序,而当我选择软件包安装程序时,出现错误"There was a problem while parsing the package"

这是我的MainActivity。

public class MainActivity extends AppCompatActivity {

private Button btnInstall;
private String path;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if(Build.VERSION.SDK_INT>=24){
        try{
            Method m = StrictMode.class.getMethod("disableDeathOnFileUriExposure");
            m.invoke(null);
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    btnInstall = (Button) findViewById(R.id.btn);
    btnInstall.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory()  + "Snapseed.apk")), "application/vnd.android.package-archive");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        }
    });
}
}

我正在尝试通过单击按钮从SD卡安装Snapseed应用程序`

提前致谢。

已解决:谢谢大家。我已经找到了问题。 我将文件保存在外部存储中,但是在代码中,我指向内部存储。

在代码中添加标志。 试试这个。 这个对我有用。

 Intent intent = new Intent(Intent.ACTION_VIEW);
 intent.setDataAndType(Uri.fromFile(new File
 (Environment.getExternalStorageDirectory() + "/download/" + "Xender.apk")),
 "application/vnd.android.package-archive");
 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 startActivity(intent);

在我的情况下,我也遇到了同样的错误。

String path = Environment.getExternalStorageDirectory().getAbsolutePath() +"/app-debug.apk";

intent.setDataAndType(Uri.fromFile(new File(path)), "application/vnd.android.package-archive");

我们必须检查现有标签和新的.apk标签应该相同。

希望这会有所帮助。

编辑

    Intent i = new Intent();
    i.setAction(Intent.ACTION_VIEW);
    i.setDataAndType(Uri.fromFile(new File(path)), "application/vnd.android.package-archive" );
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    Log.d("Lofting", "About to install new .apk");
    startActivity(i);

上面的代码对我来说很好用,我可以安装或更新现有的应用程序。

试试这个

清单文件上添加权限

  <uses-permission android:name="android.permission.INSTALL_PACKAGES" />

并尝试使用此代码安装apk

Intent newIntent = new Intent();
newIntent.putExtra(PackageUtil.INTENT_ATTR_APPLICATION_INFO, 
mPkgInfo.applicationInfo);
newIntent.setData(mPackageURI);
newIntent.setClass(this, InstallAppProgress.class);
String installerPackageName = 
getIntent().getStringExtra(Intent.EXTRA_INSTALLER_PACKAGE_NAME);
if (installerPackageName != null) {
    newIntent.putExtra(Intent.EXTRA_INSTALLER_PACKAGE_NAME, 
    installerPackageName);
}
startActivity(newIntent);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM