3

我在我的页面创建一个编辑文本,允许用户通过下载apk安装来更新应用程序。我正在使用loojp库来处理Asynctask。Android安装apk编程错误 - 包解析错误

我在活动代码:

f0textupdate.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(final View view) { 
      try { 
       view.setEnabled(false); 
       final ProgressBar f0progress = (ProgressBar) findViewById(R.id.f0progress); 
       f0progress.setProgress(0); 
       mHttpClient.get("http://google.com/apps.apk", new FileAsyncHttpResponseHandler(lastcreated) { 

        @Override 
        public void onSuccess(int i, Header[] headers, File file) { 
         //sendBroadcast(new Intent(Intent.ACTION_INSTALL_PACKAGE, Uri.fromFile(file))); 
         try { 
          String path = file.getParent()+"/"+ CfgMain.siteid+ ".apk"; 
          File dst = new File(path); 
          if (!dst.exists()) { 
           try { 
            dst.createNewFile(); 
           } catch (IOException e) { 
            e.printStackTrace(); 
           } 
          } 

          InputStream in = new FileInputStream(file); 
          OutputStream out = new FileOutputStream(dst); 

          // Transfer bytes from in to out 
          byte[] buf = new byte[1024]; 
          int len; 
          while ((len = in.read(buf)) > 0) { 
           out.write(buf, 0, len); 
          } 
          in.close(); 
          out.close(); 

          Intent installIntent = new Intent(); 
          installIntent.setAction(Intent.ACTION_INSTALL_PACKAGE); 
          installIntent.setDataAndType(Uri.fromFile(dst), "application/vnd.android.package-archive"); 
          installIntent.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true); 
          installIntent.putExtra(Intent.EXTRA_RETURN_RESULT, true); 
          installIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
          startActivity(installIntent); 

         } catch (FileNotFoundException e) { 
          e.printStackTrace(); 
         } catch (IOException e) { 
          e.printStackTrace(); 
         } 
        } 

        @Override 
        public void onFailure(int i, Header[] headers, Throwable throwable, File file) { 
         if (file.exists()) { 
          file.delete(); 
         } 
        } 

        @Override 
        public void onProgress(long bytesWritten, long totalSize) { 
         int totProgress = (int) (((float) bytesWritten * 100)/totalSize); 
         if (totProgress > 0) { 
          f0progress.setVisibility(View.VISIBLE); 
          f0progress.setProgress(totProgress); 
         } 
        } 

       }); 
      } catch (Exception ex) { 

      } 

     } 
    }); 

的Manifest.xml

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.BLUETOOTH" /> 
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> 
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> 
<uses-permission android:name="android.permission.WAKE_LOCK" /> 
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.INTERACT_ACROSS_USERS_FULL" /> 
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" /> 

它提示错误信息 “包解析错误”。

1)我需要的文件名转换按照原定的APK名称下载后,作为“文件文件”似乎存储从logcat的没有扩展名的暂时命名

更新1 错误:

05-17 11:17:19.142 4562-4562 /? W/zipro:无法打开zip'/data/data/com.M28/cache/77lori.apk':Permission denied

05-17 11:17:19.152 4562-4562 /? W/PackageParser:无法读取/data/data/com.MMM/cache/MM.apk的AndroidManifest.xml中 java.io.FileNotFoundException:AndroidManifest.xml中

更新1.1 - 改变的onSuccess()

  • 在其他权限在的Manifest.xml
+0

好吧,我得到了这个错误“无法读取AndroidManifest.xml ...” –

回答

4

添加好找到了solution从其他论坛(xamarin)和 您需要的APK保存到一个公共目录解决我的问题..。

例如:

String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)+"/MM.apk"; 
0

另一种可以与分SDK版本的问题。如果你试图安装在较低的sdk上,那么你可能会得到这种类型的错误