1

I have a function in my app where the user can copy images to the app. The app will rename the added files to numbers, counting upwards, but without any file extension.

So basically if the user adds beach.jpg it saves a copy of the file to to the path/filename: "/gallery/1"

I'm saving an entry to the sqlite database which contains the original file name (among some other infos).

I've made a sharing function so the user can select the image from the grid gallery and then share it. The problem I now have is that when I'm reading the "gallery/1" file for sharing the MIME type is null (because there's no file name extension).

So my idea was to read the file and save a copy with the original file name to some temporary folder inside my app and then share that copied file. The problem there is that the MIME type of the file is "application/octet-stream" and not "image/jpeg".

How can I re-gain the proper MIME type of the file? I could not figure out how to achieve this.

The problem now is that the apps I can select to send the image to is very limited because of the "octet-stream" MIME type. Whatsapp etc. is not selectable from the list.

My current code (snippets) look like this:

private void shareFile() {
    ShareCompat.IntentBuilder shareIntent = ShareCompat.IntentBuilder.from(activity);

    List<Uri> uris = getSelectedImagesAllGalleries();
    Log.d(TAG, "shareFile() uris size: " + uris.size());

    for (Uri uri : uris) {
        Log.d(TAG, "shareFile() URI: " + uri.getPath());
        Log.d(TAG, "shareFile() URI TYPE: " + activity.getContentResolver().getType(uri)); // is now always "application/octet-stream"
        shareIntent.setType(activity.getContentResolver().getType(uri));
        shareIntent.addStream(uri);
    }

    startActivity(shareIntent.getIntent());
}

private List<Uri> getSelectedImagesAllGalleries() {
    List<Uri> selectedImages = new ArrayList<>();
    selectedImages.addAll(getSelectedImages());
    return selectedImages;
}


private List<Uri> getSelectedImages() {
    List<Uri> uriList = new ArrayList<>();
    for (Media media : mediaList) {

        Log.d(TAG, "getSelectedImages(): " + media.getOriginalFileName() + " / " + media.isSelected());

        // Here I create a copy of the file using its original file name incl. its file extension
        File fileToShare = mediaTools.createTemporaryFileForSharing(media, activity);

        if (fileToShare.exists() && media.isSelected()) {
            Uri contentUri = FileProvider.getUriForFile(activity, activity.getPackageName(), fileToShare);
            uriList.add(contentUri);
        }
    }
    return uriList;
}

Thanks for any help in advance.

Dominik
  • 1,703
  • 6
  • 26
  • 46

1 Answers1

1

You would need inspect the file header bytes and compare the Magic Bytes in the header of the file to the known values for images to figure out what type it is.

Here is an answer to another similar question with a method of how to do this: Checking File Signature

Community
  • 1
  • 1
Carl Poole
  • 1,970
  • 2
  • 23
  • 28
  • Thanks for the link. Getting the MIME type isn't necessary the main problem I have. The problem is that I need to inject this somehow into the URL object so that the sharing function of android recognizes that object as a image type (jpg, png etc.). the problematic part in my code is: shareIntent.setType(activity.getContentResolver().getType(uri)); The "uri" object has the wrong mime type (null) there. – Dominik Mar 30 '17 at 11:34