Download überarbeitet, nun wird die Datei vom richtigen Standard Programm geöffnet
This commit is contained in:
@@ -24,6 +24,15 @@
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
<provider
|
||||
android:name="androidx.core.content.FileProvider"
|
||||
android:authorities="${applicationId}.my.provider"
|
||||
android:exported="false"
|
||||
android:grantUriPermissions="true">
|
||||
<meta-data
|
||||
android:name="android.support.FILE_PROVIDER_PATHS"
|
||||
android:resource="@xml/provider_paths"/>
|
||||
</provider>
|
||||
</application>
|
||||
|
||||
</manifest>
|
||||
@@ -15,6 +15,7 @@ import java.util.Arrays;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.core.app.ActivityCompat;
|
||||
import androidx.core.content.FileProvider;
|
||||
import de.sebse.fuplanner.MainActivity;
|
||||
import de.sebse.fuplanner.R;
|
||||
import de.sebse.fuplanner.services.KVV.types.Resource;
|
||||
@@ -23,7 +24,10 @@ import de.sebse.fuplanner.tools.RequestPermissionsResultListener;
|
||||
import de.sebse.fuplanner.tools.UtilsDate;
|
||||
import de.sebse.fuplanner.tools.logging.Logger;
|
||||
|
||||
import static android.content.Intent.normalizeMimeType;
|
||||
import static androidx.core.app.ActivityCompat.startActivityForResult;
|
||||
import static androidx.core.content.ContextCompat.checkSelfPermission;
|
||||
import static androidx.core.content.ContextCompat.startActivity;
|
||||
|
||||
public class Download {
|
||||
|
||||
@@ -31,6 +35,7 @@ public class Download {
|
||||
private final ActivityInterface activityInterface;
|
||||
private RequestedDownload requestedDownload;
|
||||
private Logger log = new Logger(this);
|
||||
static final int REQUEST_IMAGE_OPEN = 1;
|
||||
|
||||
|
||||
public Download(ContextInterface contextInterface, ActivityInterface activityInterface) {
|
||||
@@ -55,7 +60,7 @@ public class Download {
|
||||
if (file.getModifiedDate() != 0) {
|
||||
if (!message.isEmpty())
|
||||
message += "\n";
|
||||
resources.getString(R.string.last_modified_on, UtilsDate.getModifiedDateTime(contextInterface.get(), file.getModifiedDate()));
|
||||
message += resources.getString(R.string.last_modified_on, UtilsDate.getModifiedDateTime(contextInterface.get(), file.getModifiedDate()));
|
||||
}
|
||||
|
||||
alertDialogBuilder
|
||||
@@ -181,9 +186,17 @@ public class Download {
|
||||
}
|
||||
|
||||
private void fileOpen(File url){
|
||||
Uri uri = Uri.fromFile(url);
|
||||
|
||||
Intent intent = new Intent();//Intent.ACTION_VIEW
|
||||
Uri uri = FileProvider.getUriForFile(contextInterface.get(), contextInterface.get().getApplicationContext().getPackageName() + ".my.provider", url);
|
||||
|
||||
Intent intent;
|
||||
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
|
||||
intent = new Intent(Intent.ACTION_VIEW);
|
||||
} else {
|
||||
intent = new Intent();
|
||||
}
|
||||
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
||||
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
|
||||
// Check what kind of file you are trying to open, by comparing the url with extensions.
|
||||
// When the if condition is matched, plugin sets the correct intent (mime) type,
|
||||
// so Android knew what application to use to open the file
|
||||
@@ -222,13 +235,18 @@ public class Download {
|
||||
intent.setDataAndType(uri, "video/*");
|
||||
} else {
|
||||
//if you want you can also define the intent type for any other file
|
||||
|
||||
//additionally use else clause below, to manage other unknown extensions
|
||||
//in this case, Android will show all applications installed on the device
|
||||
//so you can choose which application to use
|
||||
intent.setDataAndType(uri, "*/*");
|
||||
}
|
||||
|
||||
//intent.addCategory(Intent.CATEGORY_OPENABLE);
|
||||
// Only the system receives the ACTION_OPEN_DOCUMENT, so no need to test.
|
||||
|
||||
|
||||
|
||||
|
||||
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
contextInterface.get().startActivity(intent);
|
||||
|
||||
|
||||
4
app/src/main/res/xml/provider_paths.xml
Normal file
4
app/src/main/res/xml/provider_paths.xml
Normal file
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<paths>
|
||||
<external-path name="external_files" path="."/>
|
||||
</paths>
|
||||
Reference in New Issue
Block a user