mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge.git
synced 2026-07-31 07:44:24 +02:00
Set mimeType when sharing files
This commit is contained in:
+11
-2
@@ -21,6 +21,7 @@ import android.content.Intent;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.webkit.MimeTypeMap;
|
||||
import android.widget.Filter;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.PopupMenu;
|
||||
@@ -111,8 +112,16 @@ public class FileManagerAdapter extends RecyclerView.Adapter<FileManagerAdapter.
|
||||
final int itemId = item.getItemId();
|
||||
if (itemId == R.id.file_manager_file_menu_share) {
|
||||
try {
|
||||
AndroidUtils.shareFile(mContext, file, "*/*");
|
||||
} catch (final IOException e) {
|
||||
String mimeType = null;
|
||||
final String extension = MimeTypeMap.getFileExtensionFromUrl(file.getPath());
|
||||
if (extension != null) {
|
||||
mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
|
||||
}
|
||||
if (mimeType == null) {
|
||||
mimeType = "*/*";
|
||||
}
|
||||
AndroidUtils.shareFile(mContext, file, mimeType);
|
||||
} catch (final Exception e) {
|
||||
GB.toast("Failed to share file", Toast.LENGTH_LONG, GB.ERROR, e);
|
||||
}
|
||||
return true;
|
||||
|
||||
+10
-4
@@ -720,7 +720,7 @@ class WorkoutDetailsFragment : Fragment(), MenuProvider {
|
||||
}
|
||||
|
||||
try {
|
||||
AndroidUtils.shareFile(requireContext(), gpxFile)
|
||||
AndroidUtils.shareFile(requireContext(), gpxFile, "application/gpx+xml")
|
||||
} catch (e: Exception) {
|
||||
GB.toast(
|
||||
requireContext(),
|
||||
@@ -742,7 +742,12 @@ class WorkoutDetailsFragment : Fragment(), MenuProvider {
|
||||
FileUtils.makeValidFileName("${DateTimeUtils.formatIso8601(workout.summary.startTime)}_summary.bin")
|
||||
|
||||
try {
|
||||
AndroidUtils.shareBytesAsFile(requireContext(), filename, workout.summary.rawSummaryData)
|
||||
AndroidUtils.shareBytesAsFile(
|
||||
requireContext(),
|
||||
filename,
|
||||
workout.summary.rawSummaryData,
|
||||
"application/octet-stream"
|
||||
)
|
||||
} catch (e: Exception) {
|
||||
GB.toast(
|
||||
requireContext(),
|
||||
@@ -766,7 +771,7 @@ class WorkoutDetailsFragment : Fragment(), MenuProvider {
|
||||
}
|
||||
|
||||
try {
|
||||
AndroidUtils.shareFile(requireContext(), file)
|
||||
AndroidUtils.shareFile(requireContext(), file, "application/octet-stream")
|
||||
} catch (e: Exception) {
|
||||
GB.toast(
|
||||
requireContext(),
|
||||
@@ -785,7 +790,8 @@ class WorkoutDetailsFragment : Fragment(), MenuProvider {
|
||||
AndroidUtils.shareBytesAsFile(
|
||||
requireContext(),
|
||||
filename,
|
||||
workout.data.toString().toByteArray(StandardCharsets.UTF_8)
|
||||
workout.data.toString().toByteArray(StandardCharsets.UTF_8),
|
||||
"application/json"
|
||||
)
|
||||
} catch (e: Exception) {
|
||||
GB.toast(
|
||||
|
||||
@@ -286,7 +286,10 @@ public class AndroidUtils {
|
||||
}
|
||||
}
|
||||
|
||||
public static void shareBytesAsFile(final Context context, final String name, final byte[] bytes) throws IOException {
|
||||
public static void shareBytesAsFile(final Context context,
|
||||
final String name,
|
||||
final byte[] bytes,
|
||||
final String type) throws IOException {
|
||||
final File cacheDir = context.getCacheDir();
|
||||
final File rawCacheDir = new File(cacheDir, "raw");
|
||||
rawCacheDir.mkdir();
|
||||
@@ -299,19 +302,15 @@ public class AndroidUtils {
|
||||
return;
|
||||
}
|
||||
|
||||
shareFile(context, file);
|
||||
shareFile(context, file, type);
|
||||
}
|
||||
|
||||
public static void shareFile(final Context context, final File file) throws IOException {
|
||||
public static void shareFile(final Context context, final File file, final String type) throws IOException {
|
||||
if (!file.exists()) {
|
||||
LOG.warn("File {} does not exist", file.getPath());
|
||||
return;
|
||||
}
|
||||
|
||||
shareFile(context, file, "*/*");
|
||||
}
|
||||
|
||||
public static void shareFile(final Context context, final File file, final String type) throws IOException {
|
||||
final Uri contentUri = FileProvider.getUriForFile(
|
||||
context,
|
||||
context.getApplicationContext().getPackageName() + ".screenshot_provider",
|
||||
|
||||
Reference in New Issue
Block a user