来自SD卡的Android安装.apk编程错误-包解析错误
我正试图用以下代码安装存储在下载文件夹下的外部存储中的.apk文件。我收到了一个错误对话框,其中包含一条消息:解析包时出现问题。我正在使用从google play store下载的production.apk文件和debug.apk文件。对于这两个文件,我在以编程方式安装.apk时会看到相同的错误屏幕。我可以手动安装相同的.apk文件,因此文件中没有问题。我甚至将此应用程序设置为设备所有者,但它仍然不起作用。有没有办法安装.apk文件来自SD卡的Android安装.apk编程错误-包解析错误,android,Android,我正试图用以下代码安装存储在下载文件夹下的外部存储中的.apk文件。我收到了一个错误对话框,其中包含一条消息:解析包时出现问题。我正在使用从google play store下载的production.apk文件和debug.apk文件。对于这两个文件,我在以编程方式安装.apk时会看到相同的错误屏幕。我可以手动安装相同的.apk文件,因此文件中没有问题。我甚至将此应用程序设置为设备所有者,但它仍然不起作用。有没有办法安装.apk文件 Uri mUri_ = Uri.pa
Uri mUri_ = Uri.parse(filePath);
String packageName = "com.neo.apkinstaller";
context.grantUriPermission(packageName, mUri_, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
try {
Intent appInstallerIntent = null;
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.N) {
appInstallerIntent = new Intent(Intent.ACTION_VIEW);
appInstallerIntent.setDataAndType(Uri.fromFile(new File(inst_path)), "application/vnd.android.package-archive");
} else {
Uri uri = FileProvider.getUriForFile(context, "com.neo.apkinstaller", new File(inst_path));
appInstallerIntent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
appInstallerIntent.setData(uri);
appInstallerIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
}
appInstallerIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(appInstallerIntent);
} catch (Exception e) {
e.printStackTrace();
}
urimuri=Uri.parse(filePath);
字符串packageName=“com.neo.apkinstaller”;
context.grantUriPermission(packageName、mUri、Intent.FLAG授予写入URI权限、Intent.FLAG授予读取URI权限);
试一试{
Intent appInstallerIntent=null;
如果(Build.VERSION.SDK_INT,您可以查看本youtube教程的结尾。 有一个apk是按程序安装的。 您可以忽略dropbox内容的第一部分;) 源代码是:
private void AutoUpdate (String NameOfNewApplication, String downloadPathFrom)
{
DropboxDownloadPathTo = NameOfNewApplication;
DropboxDownloadPathFrom = downloadPathFrom;
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "Download file ...", Toast.LENGTH_SHORT).show();
Thread th = new Thread(new Runnable() {
public void run() {
File file = new File(DropboxDownloadPathTo + DropboxDownloadPathFrom.substring(DropboxDownloadPathFrom.lastIndexOf('.')));
if (file.exists()) file.delete();
try {
FileOutputStream outputStream = new FileOutputStream(file);
main.dropboxAPI.getFile(DropboxDownloadPathFrom, null, outputStream, null);
getMain().runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "File successfully downloaded.", Toast.LENGTH_SHORT).show();
}
});
} catch (Exception e) {
e.printStackTrace();
}
if(file.exists())
{
while (file.length() == 0)
{
try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}
}
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
});
th.start();
}
});
}
你可以看看这个youtube教程的结尾。 有一个apk是按程序安装的。 您可以忽略dropbox内容的第一部分;) 源代码是:
private void AutoUpdate (String NameOfNewApplication, String downloadPathFrom)
{
DropboxDownloadPathTo = NameOfNewApplication;
DropboxDownloadPathFrom = downloadPathFrom;
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "Download file ...", Toast.LENGTH_SHORT).show();
Thread th = new Thread(new Runnable() {
public void run() {
File file = new File(DropboxDownloadPathTo + DropboxDownloadPathFrom.substring(DropboxDownloadPathFrom.lastIndexOf('.')));
if (file.exists()) file.delete();
try {
FileOutputStream outputStream = new FileOutputStream(file);
main.dropboxAPI.getFile(DropboxDownloadPathFrom, null, outputStream, null);
getMain().runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "File successfully downloaded.", Toast.LENGTH_SHORT).show();
}
});
} catch (Exception e) {
e.printStackTrace();
}
if(file.exists())
{
while (file.length() == 0)
{
try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}
}
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
});
th.start();
}
});
}
即使使用相同的意图调用,我在Lenovo K5 note(Android m)中也会遇到解析包错误。在Android N以上,我们需要一个文件提供程序。即使使用相同的意图调用,我在Lenovo K5 note(Android m)中也会遇到解析包错误。在Android N以上,我们需要一个文件提供程序。
private void AutoUpdate (String NameOfNewApplication, String downloadPathFrom)
{
DropboxDownloadPathTo = NameOfNewApplication;
DropboxDownloadPathFrom = downloadPathFrom;
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "Download file ...", Toast.LENGTH_SHORT).show();
Thread th = new Thread(new Runnable() {
public void run() {
File file = new File(DropboxDownloadPathTo + DropboxDownloadPathFrom.substring(DropboxDownloadPathFrom.lastIndexOf('.')));
if (file.exists()) file.delete();
try {
FileOutputStream outputStream = new FileOutputStream(file);
main.dropboxAPI.getFile(DropboxDownloadPathFrom, null, outputStream, null);
getMain().runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "File successfully downloaded.", Toast.LENGTH_SHORT).show();
}
});
} catch (Exception e) {
e.printStackTrace();
}
if(file.exists())
{
while (file.length() == 0)
{
try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}
}
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
});
th.start();
}
});
}