Friday, 15 May 2015

android - how to install an app from internal storage files -



android - how to install an app from internal storage files -

i'm trying download apk file install it. have done external storage directory when download file in local directory can't parse it.

here code on oncreate method

final downloadtask downloadtask = new downloadtask(this); downloadtask.execute("http://file.appsapk.com/wp-content/uploads/apps-2/gmail.apk","gmail.apk");

downloadtask class extends asynctask. here background task:

@override protected string doinbackground(string... surl) { file_name=surl[1]; boolean issdpresent = android.os.environment.getexternalstoragestate().equals(android.os.environment.media_mounted); if (issdpresent) { directory = new file(environment.getexternalstoragedirectory()+file.separator+"app_directory"); } else { directory = getfilesdir(); } if (!directory.exists()) directory.mkdirs(); inputstream input = null; outputstream output = null; httpurlconnection connection = null; seek { url url = new url(surl[0]); connection = (httpurlconnection) url.openconnection(); connection.connect(); if (connection.getresponsecode() != httpurlconnection.http_ok) { homecoming "server returned http " + connection.getresponsecode() + " " + connection.getresponsemessage(); } int filelength = connection.getcontentlength(); input = connection.getinputstream(); output = new fileoutputstream(directory+"/"+file_name); byte[] buffer = new byte[1024]; long total = 0; int count; while ((count = input.read(buffer)) != -1) { if (iscancelled()) { input.close(); homecoming null; } total += count; if (filelength > 0) // if total length known publishprogress((int) (total * 100 / filelength)); output.write(buffer, 0, count); } } grab (exception e) { homecoming e.tostring(); } { seek { if (input != null) input.close(); if (output != null) output.close(); } grab (ioexception ignored) { } if (connection != null) connection.disconnect(); } homecoming null; }

and post execution method runs after first 1 done downloading file:

@override protected void onpostexecute(string result) { mwakelock.release(); mprogressdialog.dismiss(); if (result != null) { toast.maketext(context,"download error: "+result, toast.length_long).show(); } else { toast.maketext(context, "file downloaded", toast.length_short).show(); file file = new file(directory, file_name); intent promptinstall = new intent(intent.action_view) .setdataandtype(uri.fromfile(file), "application/vnd.android.package-archive"); context.startactivity(promptinstall); finish(); }

it runs external storage won't run external one. why not?

try utilize openfileoutput() rather outputstream in saving file in internal storage , allow readable. http://developer.android.com/guide/topics/data/data-storage.html#filesinternal . bundle error caused permission of internal storage.

android

No comments:

Post a Comment