2013-11-25 97 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然後要麼將其複製到內部存儲的世界可讀一部分,或者要求用戶插入一個。 –