Я следую этому ответу FileProvider — IllegalArgumentException: не удалось найти настроенный корень, но ошибка все еще здесь.
Я использую Java android с SDK 30
Я использую Java android с SDK 30
p>
вот мой код
Код: Выделить всё
public void downloadFile(final Activity activity, final String url) {
try {
if (url != null && !url.isEmpty()) {
Uri uri = Uri.parse(url);
activity.registerReceiver(new MainDownloadCompleteReceiver(), new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS.toString(), "myfile.apk");
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
DownloadManager dm = (DownloadManager) activity.getSystemService(Context.DOWNLOAD_SERVICE);
dm.enqueue(request);
}
} catch (IllegalStateException e) {
Toast.makeText(activity, "Please insert an SD card to download file", Toast.LENGTH_SHORT).show();
}
}
public class MainDownloadCompleteReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
openDownloadedAttachment(context, downloadId);
}
}
private void openDownloadedAttachment(final Context context, final long downloadId) {
DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterById(downloadId);
Cursor cursor = downloadManager.query(query);
if (cursor.moveToFirst()) {
@SuppressLint("Range") int downloadStatus = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
@SuppressLint("Range") int fileSize = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
@SuppressLint("Range") String downloadLocalUri = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
@SuppressLint("Range") String downloadMimeType = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_MEDIA_TYPE));
if ((downloadStatus == DownloadManager.STATUS_SUCCESSFUL) && downloadLocalUri != null) {
openDownloadedAttachment(context, downloadLocalUri, downloadMimeType);
}
}
cursor.close();
}
/**
* Used to open the downloaded attachment.
*
* 1. Fire intent to open download file using external application.
*
* 2. Note:
* 2.a. We can't share fileUri directly to other application (because we will get FileUriExposedException from Android7.0).
* 2.b. Hence we can only share content uri with other application.
* 2.c. We must have declared FileProvider in manifest.
* 2.c. Refer - https://developer.android.com/reference/android/support/v4/content/FileProvider.html
*
* @param context Context.
* @param attachmentPath Uri of the downloaded attachment to be opened.
* @param attachmentMimeType MimeType of the downloaded attachment.
*/
private void openDownloadedAttachment(final Context context, String attachmentPath, final String attachmentMimeType) {
if(attachmentPath!=null) {
try {
File file = new File(attachmentPath);
Uri attachmentUri = FileProvider.getUriForFile(context, "myapplication.provider", file);
Intent openAttachmentIntent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
openAttachmentIntent.setDataAndType(attachmentUri, attachmentMimeType);
openAttachmentIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
openAttachmentIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(openAttachmentIntent);
} catch (ActivityNotFoundException e) {
Toast.makeText(context, "cannot open file", Toast.LENGTH_LONG).show();
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Код: Выделить всё
android:name="androidx.core.content.FileProvider"
android:authorities="myapplication.provider"
android:exported="false"
android:grantUriPermissions="true">
Код: Выделить всё
Код: Выделить всё
java.lang.IllegalArgumentException: Failed to find configured root that contains /file:/storage/emulated/0/Download/myfile.apk
at androidx.core.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:825)
at androidx.core.content.FileProvider.getUriForFile(FileProvider.java:450)
at MainActivity$MainDownloadCompleteReceiver.openDownloadedAttachment(MainActivity.java:413)
at MainActivity$MainDownloadCompleteReceiver.openDownloadedAttachment(MainActivity.java:388)
at MainActivity$MainDownloadCompleteReceiver.onReceive(MainActivity.java:371)
at android.app.LoadedApk$ReceiverDispatcher$Args.lambda$getRunnable$0$LoadedApk$ReceiverDispatcher$Args(LoadedApk.java:1556)
at android.app.-$$Lambda$LoadedApk$ReceiverDispatcher$Args$_BumDX2UKsnxLVrE6UJsJZkotuA.run(Unknown Source:2)at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Спасибо
Подробнее здесь: https://stackoverflow.com/questions/786 ... ailed-to-f