Parse error while installing downloaded .apk file

安稳与你 提交于 2019-12-24 00:45:47

问题


Hi after Two weeks again i started my research and struggling with this Error *Parse Error:*There is a problem parsing package.

Scope of my implementation is am trying to update my app from a server where i have the updated apk file and am downloading it through my app using service .Now am at edge of the Stage i can download the file from that server an i can able to install it by manually.But my scope is like after file is downloade from the Server it should automatically invoke the installing app popup window .while installing this am getting the above error.i have tried before asking this question whoever asked the same issue here.

This is my code:

public class Myservice extends Service {
String versionName;

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void onCreate() {
    Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();

}

@Override
public void onDestroy() {
    Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();

}

@Override
public void onStart(Intent intent, int startid) {
    try {
        versionName = getPackageManager().getPackageInfo(getPackageName(),
                0).versionName;
    } catch (NameNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }



     DownloadFromUrl();


}

public void DownloadFromUrl() {  //this is the downloader method
      try {
              URL url = new URL("http://61.12.5.34:10140/test/UpateTest.apk");
              String  file ="YeldiUpateTest.apk";

              long startTime = System.currentTimeMillis();

              /* Open a connection to that URL. */
              URLConnection ucon = url.openConnection();

              /*
               * Define InputStreams to read from the URLConnection.
               */
              InputStream is = ucon.getInputStream();
              BufferedInputStream bis = new BufferedInputStream(is);

              /*
               * Read bytes to the Buffer until there is nothing more to read(-1).
               */
              ByteArrayBuffer baf = new ByteArrayBuffer(50);
              int current = 0;
              while ((current = bis.read()) != -1) {
                      baf.append((byte) current);

              }

              /* Convert the Bytes read to a String. */
              FileOutputStream fos = openFileOutput(file, Context.MODE_PRIVATE);
              Log.v("wsd", "write");
              fos.write(baf.toByteArray());
              fos.close();
              Log.d("Download time", "download ready in"
                              + ((System.currentTimeMillis() - startTime) / 1000)
                              + " sec");

              install();
      } catch (IOException e) {
              Log.d("ImageManager", "Error: " + e);
      }
 }

and this is my install method():

void install()
{

     File path=getFileStreamPath("UpateTest.apk");

     Intent intent=new Intent(Intent.ACTION_VIEW);
     intent.setDataAndType(Uri.fromFile(path), "application/vnd.android.package-archive");
     intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     startActivity(intent);
}

回答1:


Finally i can able to install my apk which was downloaded from my server without any Parse Error.I tried with external storage it was happend without any error.to store the apk in internal storage and want to install means u need to change Your

FileOutputStream fos = openFileOutput(file, Context.MODE_PRIVATE)

to Context.MODE_WORLD_READABLE. and i added permissions like INSTALL_PACKAGES.



来源:https://stackoverflow.com/questions/11135680/parse-error-while-installing-downloaded-apk-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!