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)