Problem in installing a 3rd party App onto the SD card

Common bugs/problems with the Android SDK the Emulator and the ADT-Plugin.

Problem in installing a 3rd party App onto the SD card

Postby vengeance_mj » Fri May 28, 2010 8:46 am

Hi Guys,
I am trying to install a 3rd party free app through code. Here's the file which downloads the apk from the web-site, stores it on a SD Card.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class DownloadFile extends Activity implements View.OnClickListener {
  2.  
  3.         private static final int WAIT_DIALOG_KEY = 123;
  4.         private ProgressDialog waitDialog;
  5.         private String url, root = "/data/data/wms.apps.android/", filename;
  6.         private boolean isDownloaded = false;
  7.         private Bitmap bitmap;
  8.         private ImageView image;
  9.         private File downloadFile;
  10.         private TextView tv;
  11.  
  12.         private class DownloadTask extends AsyncTask<Void, String, Void> {
  13.  
  14.                 @Override
  15.                 protected Void doInBackground(Void... arg0) {
  16.                         // TODO Auto-generated method stub
  17.                         try {
  18.                                 URL imageUrl = new URL(url);
  19.                                 InputStream in = openHttpConnection(imageUrl);
  20.                                 // bitmap = BitmapFactory.decodeStream(in);
  21.                                 ByteArrayBuffer buffer = new ByteArrayBuffer(500);
  22.  
  23.                                 int byteRead = 0;
  24.  
  25.                                 while ((byteRead = in.read()) != -1) {
  26.                                         buffer.append(byteRead);
  27.                                 }
  28.                                 String filename = Environment.getExternalStorageDirectory()
  29.                                                 + "/megan.apk";
  30.                                 downloadFile = new File(filename);
  31.                                 // downloadFile = new File(root, "test.apk");
  32.                                 FileOutputStream out = new FileOutputStream(downloadFile);
  33.                                 out.write(buffer.toByteArray());
  34.                                 in.close();
  35.                                 out.close();
  36.                                 isDownloaded = true;
  37.  
  38.                         } catch (Exception e) {
  39.  
  40.                         }
  41.                         return null;
  42.                 }
  43.  
  44.                 private InputStream openHttpConnection(URL aUrl) {
  45.                         InputStream in = null;
  46.                         try {
  47.                                 URLConnection connection = aUrl.openConnection();
  48.  
  49.                                 HttpURLConnection httpConnection = (HttpURLConnection) connection;
  50.                                 httpConnection.setAllowUserInteraction(false);
  51.                                 httpConnection.setInstanceFollowRedirects(true);
  52.                                 httpConnection.setRequestMethod("GET");
  53.  
  54.                                 httpConnection.connect();
  55.  
  56.                                 in = httpConnection.getInputStream();
  57.                         } catch (Exception e) {
  58.  
  59.                         }
  60.                         return in;
  61.                 }
  62.  
  63.                 protected void onPreExecute() {
  64.                         showDialog(WAIT_DIALOG_KEY);
  65.                 }
  66.  
  67.                 protected void onPostExecute(Void voids) {
  68.                         // image.setImageBitmap(bitmap);
  69.                         if (isDownloaded)
  70.                                 tv.setText("APK downloaded");
  71.                         waitDialog.dismiss();
  72.                         // Toast.makeText(DownloadFile.this, "Done", Toast.LENGTH_SHORT)
  73.                         // .show();
  74.  
  75.                 }
  76.         }
  77.  
  78.         protected Dialog onCreateDialog(int id) {
  79.                 switch (id) {
  80.                 case WAIT_DIALOG_KEY: {
  81.                         waitDialog = new ProgressDialog(this);
  82.                         waitDialog.setMessage("Please wait while loading...");
  83.                         waitDialog.setIndeterminate(true);
  84.                         waitDialog.setCancelable(true);
  85.                         return waitDialog;
  86.                 }
  87.                 }
  88.                 return null;
  89.         }
  90.  
  91.         @Override
  92.         public void onCreate(Bundle savedInstanceState) {
  93.                 super.onCreate(savedInstanceState);
  94.                 setContentView(R.layout.list);
  95.                 Bundle intentData = this.getIntent().getExtras();
  96.                 url = intentData.getString("url");
  97.         }
  98.  
  99.         public void onStart() {
  100.                 super.onStart();
  101.                 new DownloadTask().execute();
  102.                 tv = (TextView) findViewById(R.id.label);
  103.                 image = (ImageView) findViewById(R.id.image);
  104.                 Button aButton = (Button) findViewById(R.id.install);
  105.                 aButton.setOnClickListener(this);
  106.  
  107.                 if (isDownloaded)
  108.                         tv.setText("Image downloaded");
  109.                 else
  110.                         tv.setText("Image download failed");
  111.  
  112.         }
  113.  
  114.  
Parsed in 0.041 seconds, using GeSHi 1.0.8.4


Then when the user clicks on the Install button I am running this code
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.         public void onClick(View arg0) {
  3.                 // TODO Auto-generated method stub
  4.                 boolean success;
  5.                 int result = Settings.Secure.getInt(getContentResolver(),
  6.                                 Settings.Secure.INSTALL_NON_MARKET_APPS, 0);
  7.                 if (result == 0)
  8.                         success = Settings.Secure.putString(getContentResolver(),
  9.                                         Settings.Secure.INSTALL_NON_MARKET_APPS, "1");
  10.                 Intent intent = new Intent();
  11.                 intent.setAction(android.content.Intent.ACTION_VIEW);
  12.                 intent.setDataAndType(Uri.fromFile(downloadFile),
  13.                                 "application/vnd.android.package-archive");
  14.                 startActivity(intent);
  15.         }
  16. } //End of class file
  17.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


I read somewhere in order to install 3rd party apps we need to set INSTALL_NON_MARKET_APPS. For setting these settings , is it needed to use any permission in the manifest file?
Also, when I did a logcat the error was
Code: Select all
Unable to read AndroidManifest.xml of /sdcard/test.apk and then it threw a IOException


What exactly could be the problem? I would very much apreciate it if somebody would tell me what are the steps needed to install a 3rd party application.

Thanks
vengeance_mj
Senior Developer
Senior Developer
 
Posts: 174
Joined: Thu Oct 29, 2009 2:32 am

Top

Re: Problem in installing a 3rd party App onto the SD card

Postby solider » Mon Aug 02, 2010 5:58 am

Hi, this problem can be solved by adding this code to AndroidManifest file:
Code: Select all
<uses-permission android:name="android.permission.INSTALL_PACKAGES" />


If after adding this code, your program not start sucess, please check .APK file on sdcard :)
solider
Freshman
Freshman
 
Posts: 4
Joined: Fri Jul 16, 2010 8:04 am

Top

Return to SDK/ADT/Emulator Problems

Who is online

Users browsing this forum: No registered users and 6 guests