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" />
|
<category android:name="android.intent.category.LAUNCHER" />
|
||||||
</intent-filter>
|
</intent-filter>
|
||||||
</activity>
|
</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>
|
</application>
|
||||||
|
|
||||||
</manifest>
|
</manifest>
|
||||||
@@ -15,6 +15,7 @@ import java.util.Arrays;
|
|||||||
|
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
import androidx.core.app.ActivityCompat;
|
import androidx.core.app.ActivityCompat;
|
||||||
|
import androidx.core.content.FileProvider;
|
||||||
import de.sebse.fuplanner.MainActivity;
|
import de.sebse.fuplanner.MainActivity;
|
||||||
import de.sebse.fuplanner.R;
|
import de.sebse.fuplanner.R;
|
||||||
import de.sebse.fuplanner.services.KVV.types.Resource;
|
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.UtilsDate;
|
||||||
import de.sebse.fuplanner.tools.logging.Logger;
|
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.checkSelfPermission;
|
||||||
|
import static androidx.core.content.ContextCompat.startActivity;
|
||||||
|
|
||||||
public class Download {
|
public class Download {
|
||||||
|
|
||||||
@@ -31,6 +35,7 @@ public class Download {
|
|||||||
private final ActivityInterface activityInterface;
|
private final ActivityInterface activityInterface;
|
||||||
private RequestedDownload requestedDownload;
|
private RequestedDownload requestedDownload;
|
||||||
private Logger log = new Logger(this);
|
private Logger log = new Logger(this);
|
||||||
|
static final int REQUEST_IMAGE_OPEN = 1;
|
||||||
|
|
||||||
|
|
||||||
public Download(ContextInterface contextInterface, ActivityInterface activityInterface) {
|
public Download(ContextInterface contextInterface, ActivityInterface activityInterface) {
|
||||||
@@ -55,7 +60,7 @@ public class Download {
|
|||||||
if (file.getModifiedDate() != 0) {
|
if (file.getModifiedDate() != 0) {
|
||||||
if (!message.isEmpty())
|
if (!message.isEmpty())
|
||||||
message += "\n";
|
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
|
alertDialogBuilder
|
||||||
@@ -181,9 +186,17 @@ public class Download {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void fileOpen(File url){
|
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.
|
// 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,
|
// When the if condition is matched, plugin sets the correct intent (mime) type,
|
||||||
// so Android knew what application to use to open the file
|
// so Android knew what application to use to open the file
|
||||||
@@ -222,13 +235,18 @@ public class Download {
|
|||||||
intent.setDataAndType(uri, "video/*");
|
intent.setDataAndType(uri, "video/*");
|
||||||
} else {
|
} else {
|
||||||
//if you want you can also define the intent type for any other file
|
//if you want you can also define the intent type for any other file
|
||||||
|
|
||||||
//additionally use else clause below, to manage other unknown extensions
|
//additionally use else clause below, to manage other unknown extensions
|
||||||
//in this case, Android will show all applications installed on the device
|
//in this case, Android will show all applications installed on the device
|
||||||
//so you can choose which application to use
|
//so you can choose which application to use
|
||||||
intent.setDataAndType(uri, "*/*");
|
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);
|
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||||
contextInterface.get().startActivity(intent);
|
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