Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed and added support for dataUri in form data #33675

Closed
wants to merge 9 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

import android.content.Context;
import android.net.Uri;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;
import androidx.annotation.Nullable;
import com.facebook.common.logging.FLog;
import com.facebook.react.common.ReactConstants;
Expand All @@ -18,6 +21,7 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ByteArrayInputStream;
import java.io.OutputStream;
import java.net.URL;
import java.nio.channels.Channels;
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);
InputStream inputStream = new ByteArrayInputStream(bytes.toByteArray());
return inputStream;
}

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