Reputation: 2066
I am developing an app that is not going to be located on the Google Play, I need, update that apk by downloading the newest apk that I have uploaded manually from any random downloadable link and then, install it when.
I followed this thread Update an Android app (without Google Play)
I ran into some problem, the app crashed because "file://" scheme is now not allowed to be attached with Intent on targetSdkVersion 24 and higher and indeed my app is targeted to higher sdk versions.
I then followed this blog to fix the issue: https://inthecheesefactory.com/blog/how-to-share-access-to-file-with-fileprovider-on-android-nougat/en I implemented a FileProvider as it described in the blog like that:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
Then I added the xml dir and created the file provider_paths.xml
I just followed everything it said, here is my java code:
public class MainActivity extends AppCompatActivity {
Uri fileUriGlobal;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyTask task = new MyTask(this);
task.execute("download1325.mediafire.com/034htxjngoeg/iypqfw37umzo85a/imgapk.apk");
}
class MyTask extends AsyncTask<String, Integer, Void> {
Context context;
public MyTask(Context context) {
this.context = context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(String... strings) {
String link = strings[0];
try {
URL url = new URL(link);
URLConnection connection = url.openConnection();
connection.connect();
int fileLength = connection.getContentLength();
// download the file
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = openFileOutput("imgapk.apk", MODE_PRIVATE);
byte data[] = new byte[6000];
int count;
while ((count = input.read(data)) != -1) {
Log.d("KingArmstring", "doInBackground: " + count);
output.write(data, 0, count);
}
File apkFile = new File(getFilesDir(), "imgapk.apk");
fileUriGlobal = FileProvider.getUriForFile(context,
BuildConfig.APPLICATION_ID + ".provider",
apkFile);
output.flush();
output.close();
input.close();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
Intent i = new Intent();
i.setAction(Intent.ACTION_VIEW);
i.setDataAndType(fileUriGlobal, "application/vnd.android.package-archive");
context.startActivity(i);
}
}
}
after all what I did I still can't solve it and I am getting this crash:
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:318)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761)
Caused by: java.lang.IllegalArgumentException: Failed to find configured root that contains /data/data/com.microdoers.updateapk/files/imgapk.apk
at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:739)
at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:418)
at com.microdoers.updateapk.MainActivity$MyTask.doInBackground(MainActivity.java:78)
at com.microdoers.updateapk.MainActivity$MyTask.doInBackground(MainActivity.java:37)
at android.os.AsyncTask$2.call(AsyncTask.java:304)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761)
Upvotes: 0
Views: 239
Reputation: 2066
I have solved the problem, half of the solution is the same as @SRB bans, but it did not work by alone, in the onPostExecute() method instead of:
File file = new File(filepath);
Uri mUri = FileProvider.getUriForFile(context,
BuildConfig.APPLICATION_ID + ".provider",file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(mUri, "application/vnd.android.package-archive");
startActivity(intent);
I used:
File file = new File(filepath);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Log.d("KingArmstring", "onPostExecute: 24 or higher");
Uri apkUri = FileProvider.getUriForFile(((Activity)context), BuildConfig.APPLICATION_ID + ".provider", file);
Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
intent.setData(apkUri);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
((Activity)context).startActivity(intent);
} else {
Log.d("KingArmstring", "onPostExecute: 23 or lower");
Uri apkUri = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
((Activity)context).startActivity(intent);
}
Upvotes: 0
Reputation: 3134
This is how i did it...
// DownloadFile AsyncTask
private class DownloadFile extends AsyncTask<String, Integer, String> {
ProgressDialog mProgressDialog;
String filepath;
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create progress dialog
mProgressDialog = new ProgressDialog(context);
// Set your progress dialog Title
mProgressDialog.setTitle("Downloading Updates!");
// Set your progress dialog Message
mProgressDialog.setMessage("Click Install when done...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.setMax(100);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setCancelable(false);
// Show progress dialog
mProgressDialog.show();
}
@Override
protected String doInBackground(String... Url) {
try {
String app_url = Url[0];
String f_name = "";
if (app_url.contains("/")) {
String temp[] = app_url.split("/");
f_name = temp[temp.length - 1];
}
URL url = new URL(app_url);
URLConnection connection = url.openConnection();
connection.connect();
// Detect the file length
int fileLength = connection.getContentLength();
// Locate storage location
filepath = Environment.getExternalStorageDirectory()
.getPath() + "/" + f_name;
// Download the file
InputStream input = new BufferedInputStream(url.openStream());
// Save the downloaded file
OutputStream output = new FileOutputStream(filepath);
byte data[] = new byte[1024];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
total += count;
// Publish the progress
publishProgress((int) (total * 100 / fileLength));
output.write(data, 0, count);
}
// Close connection
output.flush();
output.close();
input.close();
} catch (Exception e) {
// Error Log
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onProgressUpdate(Integer... progress) {
super.onProgressUpdate(progress);
mProgressDialog.setProgress(progress[0]);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
mProgressDialog.dismiss();
if (filepath != null) {
File file = new File(filepath);
Uri mUri = FileProvider.getUriForFile(context,
BuildConfig.APPLICATION_ID + ".provider",file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(mUri, "application/vnd.android.package-archive");
startActivity(intent);
}
}
}
Upvotes: 2