Bridge developed

This commit is contained in:
ShriPrakashBajpai
2023-09-22 21:15:08 +05:30
parent 504f4588db
commit 10cd908b46
3 changed files with 158 additions and 17 deletions

View File

@@ -0,0 +1,91 @@
package com.avapp;
import android.annotation.SuppressLint;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Base64;
import android.util.Log;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class Base64ToContentUri {
public static Uri getUriFromBase64(Context context, String base64Image) {
// Decode base64 string to bitmap
Bitmap bitmap = decodeBase64ToBitmap(base64Image);
// Save the bitmap to a file and get the content URI for that file
return saveBitmapAsImage(context, bitmap);
}
private static Bitmap decodeBase64ToBitmap(String base64Image) {
byte[] decodedBytes = Base64.decode(base64Image, Base64.DEFAULT);
return BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.length);
}
private static Uri saveBitmapAsImage(Context context, Bitmap bitmap) {
File imagesDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DCIM), "YourAppImages");
if (!imagesDir.exists()) {
if (!imagesDir.mkdirs()) {
Log.e("Base64ToContentUri", "Failed to create directory");
return null;
}
}
String fileName = System.currentTimeMillis() + ".jpg";
File imageFile = new File(imagesDir, fileName);
try (OutputStream fOut = new FileOutputStream(imageFile)) {
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
fOut.flush();
} catch (IOException e) {
Log.e("Base64ToContentUri", "Failed to save image file: " + e.getMessage());
return null;
}
// Update the media store to include the image
MediaScannerConnection.scanFile(context, new String[]{imageFile.getAbsolutePath()}, null, null);
// Return the content URI for the saved image
return getImageContentUri(context, imageFile);
}
private static Uri getImageContentUri(Context context, File imageFile) {
String filePath = imageFile.getAbsolutePath();
Cursor cursor = context.getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
new String[]{MediaStore.Images.Media._ID},
MediaStore.Images.Media.DATA + "=? ",
new String[]{filePath}, null);
if (cursor != null && cursor.moveToFirst()) {
@SuppressLint("Range") int id = cursor.getInt(cursor.getColumnIndex(MediaStore.MediaColumns._ID));
cursor.close();
return Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "" + id);
} else {
if (imageFile.exists()) {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DATA, filePath);
return context.getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
} else {
return null;
}
}
}
}

View File

@@ -15,6 +15,10 @@ import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import android.content.pm.PackageInfo;
import android.os.Environment;
import android.util.Base64;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONObject;
@@ -23,7 +27,9 @@ import java.util.List;
import android.net.Uri;
import android.util.Log;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class DeviceUtilsModule extends ReactContextBaseJavaModule implements ActivityEventListener {
private ReactApplicationContext RNContext;
@@ -96,24 +102,66 @@ public class DeviceUtilsModule extends ReactContextBaseJavaModule implements Act
// return;
// }
@ReactMethod
public void sendFeedbackToWhatsapp(String encodedMessage, String encodedImageUri, String mimeType) {
Log.d("Native Function called","string eg");
private static File convertBase64ToFile(Context context,String base64Data) {
try {
// Convert Base64 to byte array
byte[] decodedBytes = Base64.decode(base64Data, Base64.DEFAULT);
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, encodedMessage);
sendIntent.setType(mimeType);
// Create a temporary file to store the Base64 data
// Convert the encodedImageUri to a URI and set it as an EXTRA_STREAM
Uri uri = Uri.parse(encodedImageUri);
sendIntent.putExtra(Intent.EXTRA_STREAM, uri);
File outputDir = context.getCacheDir();
File file = File.createTempFile("temp_image", ".jpg", outputDir);
sendIntent.setPackage("com.whatsapp");
// Write the byte array to the file
FileOutputStream fos = new FileOutputStream(file);
fos.write(decodedBytes);
fos.flush();
fos.close();
// Start the activity
getCurrentActivity().startActivity(sendIntent);
return;
return file;
} catch (IOException e) {
Log.e("ShareUtils", "Failed to convert Base64 to file: " + e.getMessage());
return null;
}
}
@ReactMethod
public void sendFeedbackToWhatsapp(String encodedMessage, String encodedImageUri, String mimeType) {
Log.d("Native Function called","string eg");
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, encodedMessage);
sendIntent.setType("image/*");
File imageFile = convertBase64ToFile(getReactApplicationContext(),encodedImageUri);
Uri fileUri = Uri.fromFile(imageFile);
//Uri imageUri = Base64ToContentUri.getUriFromBase64(getReactApplicationContext(), encodedImageUri);
// Convert the encodedImageUri to a URI and set it as an EXTRA_STREAM
//Uri uri = Uri.parse(encodedImageUri);
sendIntent.putExtra(Intent.EXTRA_STREAM, fileUri);
Log.d("base64 data", fileUri.toString());
sendIntent.setPackage("com.whatsapp");
// Start the activity
getCurrentActivity().startActivity(sendIntent);
return;
}
@ReactMethod
public String helloWorld() {
Log.d("debug", "hello world called");
Toast.makeText(getReactApplicationContext(), "Hello from Native Module!", Toast.LENGTH_SHORT).show();
return "hello";
}
}

View File

@@ -119,7 +119,7 @@ const sendToWhatsapp = (feedbackItem: IFeedback, caseDetails: any) => {
return resp.readFile('base64');
})
.then((base64Data: any) => {
var imageUrl = 'data:image/png;base64,' + base64Data;
var imageUrl = base64Data;
const encodedMessage = encodeURIComponent(message);
const encodedImageUri = encodeURIComponent(imageUrl);
@@ -173,7 +173,9 @@ const sendToWhatsapp = (feedbackItem: IFeedback, caseDetails: any) => {
// return Linking.openURL(sendIntent);
const mimeType = 'image/*';
console.log(DeviceUtilsModule);
DeviceUtilsModule.sendFeedbackToWhatsApp(message, imageUri, mimeType);
DeviceUtilsModule.sendFeedbackToWhatsapp(message, imageUrl, mimeType);
// DeviceUtilsModule.helloWorld();
});
};