0

here is my onActivityResult method -


public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == PICK_VIDEO_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
      // upload cachedVideo
      Uri originalDataUri = data.getData();

      Timber.d("Receive image from: %s", originalDataUri);
      File file = new File(originalDataUri.toString());
      try {

        //here is the line of the problem

        File compressedFile = new Compressor(getContext()).compressToFile(file); // <--here I get "file not found expection" 
        cachedLocalImageUri = Uri.fromFile(compressedFile);
      } catch (IOException e) {
        e.printStackTrace();
      }
      mImageSent.setImageURI(cachedLocalImageUri);
    }
  }


I am using the following github library -

https://github.com/zetbaitsu/Compressor

and when trying to create a new object out of it I get "file not found expection" when trying to create a new file from my java.net.Uri from onActivityResult, but the thing is that for sure there is a Uri at that place because I can fill an imageview from that path.

So, what am I missing?

Alon Shlider
  • 1,187
  • 1
  • 16
  • 46
  • you can get your answer from here: https://stackoverflow.com/questions/39313752/how-to-pick-image-for-crop-from-camera-or-gallery-in-android-7-0 , https://stackoverflow.com/questions/39242026/fileuriexposedexception-in-android-n-with-camera – Shweta Chauhan Sep 01 '19 at 13:05
  • let me know if you still face issue I will post answer here – Shweta Chauhan Sep 01 '19 at 13:06
  • 1
    @ShwetaChauhan I would be happy if you could answer here, that post has too many answers and they are old also so I would be happy for a new one (androidX compatible) – Alon Shlider Sep 01 '19 at 13:19
  • can you please debug your code and say what you sre getting in "compressedFile". I want to sure that your issue is for content path. – Shweta Chauhan Sep 01 '19 at 13:41
  • this line 'compressor.compressToFile(file)' is giving me "file not found exepction", though when evaluating the file value in debug it has a value of a valid file. – Alon Shlider Sep 01 '19 at 13:52
  • "though when evaluating the file value in debug it has a value of a valid file" -- that would only be the case if the `Uri` has a `file` scheme, and even then it will not work on Android 10 and higher. Your chosen library does not support `Uri` or `OutputStream`. Perhaps you can contribute changes to that library to support those things, or perhaps you can use that library's code as inspiration for your own in-project implementation that can save to an `OutputStream`. Given the `Uri`, use `ContentResolver` and `openOutputStream()` to get the `OutputStream` to use. – CommonsWare Sep 01 '19 at 13:54
  • So, do you know a more modern library that I can just get the data out of the intent and get a valid URI that I can create a file from? I really don't want to create my own library haha. – Alon Shlider Sep 01 '19 at 13:56
  • Please use @ notation to post a comment reply to a previous commenter. In terms of your code, [you have very little direct filesystem access on Android 10+](https://commonsware.com/blog/2019/06/07/death-external-storage-end-saga.html). It is also unclear what the whole `Compressor` thing is for. Besides showing an image in the `ImageView` (in ways that freeze your UI, but that's a separate problem), what is the objective of the work that you are doing here in `onActivityResult()`? – CommonsWare Sep 01 '19 at 14:06
  • @CommonsWare I need to compress the image (file) in order to fit it to firebase functions allowed size for notification images – Alon Shlider Sep 01 '19 at 14:09
  • @CommonsWare the purpose of the compressor library is, as it's name suggests, to compress my image before uploading the file to firebase storage. – Alon Shlider Sep 01 '19 at 14:15
  • Most likely, the image is already compressed. JPEG, PNG, and GIF images are all stored in compressed form. – CommonsWare Sep 01 '19 at 14:16
  • @CommonsWare that is wrong. the images come directly from my camera original size, which anywhere between 2-3MBs. that is way more than firebase allows for fcm notification images. Do you know of any way to reduce dramatically the size of the image selected? – Alon Shlider Sep 01 '19 at 14:17
  • "that is wrong" -- no, it is not. "the images come directly from my camera" -- most likely, they are in JPEG format, which is a compressed format. "Do you know of any way to reduce dramatically the size of the image selected?" -- the most common solution is to reduce the resolution of the image. See [this](https://stackoverflow.com/q/9360976/115145) and [this](https://stackoverflow.com/q/3331527/115145). Given the scaled `Bitmap`, you can call `compress()` on it to write it to a `FileOutputStream` on some file that you control (e.g., in `getCacheDir()`). – CommonsWare Sep 01 '19 at 14:24

0 Answers0