Skip to content

Commit

Permalink
Fixed and added support for dataUri in form data (#33675)
Browse files Browse the repository at this point in the history
Summary:
Continuation of #33548
 Fixes #25790. The issue resulted because the [getFileInputStream() method of RequestBodyUtil failed to return an inputStream of the given dataUri ](https://github.com/facebook/react-native/blob/16397e0d3c0dd3374ddb642599725ca41f092a8a/ReactAndroid/src/main/java/com/facebook/react/modules/network/NetworkingModule.java#L427) This happened because the openInputStream() method of [context.getContentResolver() expects a content or a file](https://developer.android.com/reference/android/content/ContentResolver#openInputStream(android.net.Uri)) in its parameter but instead received dataUri which resulted in FileNotFoundException.

![issue](https://user-images.githubusercontent.com/38756320/161345967-fd79d3e2-54a8-4a0e-8a6b-189ce9883a78.jpeg)

**Solution**:

I've now converted the dataUri to bitmap and then attached an inputStream to the compressed bitmap. This way we won't have to store the image temporarily. I think converting the dataUri to Bitmap is necessary as there is no method that lets us convert the dataUri to inputStream directly. And regarding large size images, converting them to bitmap is the only(efficient) step I can think of, as the conversion is handled by in-built java function.

This issue has been unresolved for quite some time now, so resolving this PR would be greatly appreciated.
## Changelog

<!-- Help reviewers and the release process by writing your own changelog entry. For an example, see:
https://github.com/facebook/react-native/wiki/Changelog
-->

[Android] [Added] - Support for dataUri in form data

Pull Request resolved: #33675

Reviewed By: christophpurrer, mdvacca

Differential Revision: D36205586

Pulled By: cortinico

fbshipit-source-id: bfc83efcec0b2fcb1df42e4bf1d43c966de8f40e
  • Loading branch information
hetanthakkar authored and facebook-github-bot committed May 10, 2022
1 parent 2e720c3 commit c663c0e
Showing 1 changed file with 14 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@
package com.facebook.react.modules.network;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.util.Base64;
import androidx.annotation.Nullable;
import com.facebook.common.logging.FLog;
import com.facebook.react.common.ReactConstants;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
Expand Down Expand Up @@ -58,6 +62,16 @@ public static boolean isGzipEncoding(@Nullable final String encodingType) {
if (fileContentUri.getScheme().startsWith("http")) {
return getDownloadFileInputStream(context, fileContentUri);
}

if (fileContentUriStr.startsWith("data:")) {
byte[] decodedDataUrString = Base64.decode(fileContentUriStr.split(",")[1], Base64.DEFAULT);
Bitmap bitMap =
BitmapFactory.decodeByteArray(decodedDataUrString, 0, decodedDataUrString.length);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitMap.compress(Bitmap.CompressFormat.PNG, 0, bytes);
return new ByteArrayInputStream(bytes.toByteArray());
}

return context.getContentResolver().openInputStream(fileContentUri);
} catch (Exception e) {
FLog.e(ReactConstants.TAG, "Could not retrieve file for contentUri " + fileContentUriStr, e);
Expand Down

0 comments on commit c663c0e

Please sign in to comment.