2013-11-25 38 views
-1

我已经建立在Eclipse的应用程序,我已经放在这个应用程序的资源文件夹内的一些其他.apk文件安装apk文件放入资产文件夹内的文件。我已经增加了一些图像按钮的onClick我想安装.apk应用程序放在资产文件夹内,而不将其复制到SD卡外。我正在尝试下面的代码,但我得到了解析错误。下面是代码:没有将其拷贝到SD卡

ImageButton animal1 = (ImageButton)findViewById(R.id.imageButton1); 
animal1.setOnClickListener(new OnClickListener() { 

@Override 
public void onClick(View v) { 
    // TODO Auto-generated method stub 
    AssetManager assetManager = getAssets(); 
    InputStream in = null; 
    OutputStream out = null; 
    try { 
     in = assetManager.open("Animal Game.apk"); 
     out = new FileOutputStream("/data/data/com.mypack.myproj/Animal Game.apk"); 
     byte[] buffer = new byte[1024]; 

     int read; 
     while((read = in.read(buffer)) != -1) { 
       out.write(buffer, 0, read); 
     } 
     in.close(); 
     in = null; 
     out.flush(); 
     out.close(); 
     out = null; 
     Intent intent = new Intent(Intent.ACTION_VIEW); 
     intent.setDataAndType(Uri.fromFile(newFile("/data/data/com.mypack.myproj/files/Animal Game.apk")), "application/vnd.android.package-archive"); 

     startActivity(intent); 
    } catch(Exception e) { 
      Toast.makeText(getBaseContext(), "Error: "+e, Toast.LENGTH_LONG).show(); 
     } 
    } 
}); 

我得到的错误解析错误:

There was a problem parsing the package.

请人帮我解决这个错误

+0

是的,它不能分析您的APK。那么,你的APK有效吗? – Raptor

+0

雅我'APK'是有效的,并且它的'AIR application'我已经在我的设备上安装了这个'APK' ..有什么问题? – Sush19

回答

1

您必须将APK文件复制到之前将它传递给PackageManager,作为PackageManager在一个单独的进程作为一个独立的应用程序运行,而不能访问你的应用程序的内部文件SD卡。

+0

如果设备中没有SD卡,该怎么办......? – Sush19

+0

@ Sush19然后要么将其复制到内部存储的世界可读一部分,或者要求用户插入一个。 –