fix: add intent-handling and automated stream-closing to Java
This commit is contained in:
@@ -93,6 +93,8 @@ public class MainActivity extends BridgeActivity {
|
|||||||
String action = intent.getAction();
|
String action = intent.getAction();
|
||||||
String type = intent.getType();
|
String type = intent.getType();
|
||||||
|
|
||||||
|
boolean handled = false;
|
||||||
|
|
||||||
// Handle single image share
|
// Handle single image share
|
||||||
if (Intent.ACTION_SEND.equals(action) && type != null && type.startsWith("image/")) {
|
if (Intent.ACTION_SEND.equals(action) && type != null && type.startsWith("image/")) {
|
||||||
Uri imageUri;
|
Uri imageUri;
|
||||||
@@ -108,6 +110,7 @@ public class MainActivity extends BridgeActivity {
|
|||||||
if (imageUri != null) {
|
if (imageUri != null) {
|
||||||
String fileName = intent.getStringExtra(Intent.EXTRA_TEXT);
|
String fileName = intent.getStringExtra(Intent.EXTRA_TEXT);
|
||||||
processSharedImage(imageUri, fileName);
|
processSharedImage(imageUri, fileName);
|
||||||
|
handled = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Handle multiple images share (we'll just process the first one)
|
// Handle multiple images share (we'll just process the first one)
|
||||||
@@ -124,24 +127,54 @@ public class MainActivity extends BridgeActivity {
|
|||||||
}
|
}
|
||||||
if (imageUris != null && !imageUris.isEmpty()) {
|
if (imageUris != null && !imageUris.isEmpty()) {
|
||||||
processSharedImage(imageUris.get(0), null);
|
processSharedImage(imageUris.get(0), null);
|
||||||
|
handled = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Clear the intent after handling to release URI permissions and prevent
|
||||||
|
// network issues in WebView. This is critical for preventing the WebView
|
||||||
|
// from losing network connectivity after processing shared content.
|
||||||
|
if (handled) {
|
||||||
|
intent.setAction(null);
|
||||||
|
intent.setData(null);
|
||||||
|
intent.removeExtra(Intent.EXTRA_STREAM);
|
||||||
|
intent.setType(null);
|
||||||
|
setIntent(new Intent());
|
||||||
|
Log.d(TAG, "Cleared share intent after processing");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Process a shared image: read it, convert to base64, and write to temp file
|
* Process a shared image: read it, convert to base64, and write to temp file
|
||||||
|
* Uses try-with-resources to ensure proper stream cleanup and prevent network issues
|
||||||
*/
|
*/
|
||||||
private void processSharedImage(Uri imageUri, String fileName) {
|
private void processSharedImage(Uri imageUri, String fileName) {
|
||||||
try {
|
// Extract filename from URI or use default (do this before opening streams)
|
||||||
// Read image data from URI
|
String actualFileName = fileName;
|
||||||
InputStream inputStream = getContentResolver().openInputStream(imageUri);
|
if (actualFileName == null || actualFileName.isEmpty()) {
|
||||||
|
String path = imageUri.getPath();
|
||||||
|
if (path != null) {
|
||||||
|
int lastSlash = path.lastIndexOf('/');
|
||||||
|
if (lastSlash >= 0 && lastSlash < path.length() - 1) {
|
||||||
|
actualFileName = path.substring(lastSlash + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (actualFileName == null || actualFileName.isEmpty()) {
|
||||||
|
actualFileName = "shared-image.jpg";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use try-with-resources to ensure streams are properly closed
|
||||||
|
// This is critical to prevent resource leaks that can affect WebView networking
|
||||||
|
try (InputStream inputStream = getContentResolver().openInputStream(imageUri);
|
||||||
|
ByteArrayOutputStream buffer = new ByteArrayOutputStream()) {
|
||||||
|
|
||||||
if (inputStream == null) {
|
if (inputStream == null) {
|
||||||
Log.e(TAG, "Failed to open input stream for shared image");
|
Log.e(TAG, "Failed to open input stream for shared image");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read image bytes
|
// Read image bytes
|
||||||
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
|
|
||||||
byte[] data = new byte[8192];
|
byte[] data = new byte[8192];
|
||||||
int nRead;
|
int nRead;
|
||||||
while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
|
while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
|
||||||
@@ -149,26 +182,10 @@ public class MainActivity extends BridgeActivity {
|
|||||||
}
|
}
|
||||||
buffer.flush();
|
buffer.flush();
|
||||||
byte[] imageBytes = buffer.toByteArray();
|
byte[] imageBytes = buffer.toByteArray();
|
||||||
inputStream.close();
|
|
||||||
|
|
||||||
// Convert to base64
|
// Convert to base64
|
||||||
String base64String = Base64.encodeToString(imageBytes, Base64.NO_WRAP);
|
String base64String = Base64.encodeToString(imageBytes, Base64.NO_WRAP);
|
||||||
|
|
||||||
// Extract filename from URI or use default
|
|
||||||
String actualFileName = fileName;
|
|
||||||
if (actualFileName == null || actualFileName.isEmpty()) {
|
|
||||||
String path = imageUri.getPath();
|
|
||||||
if (path != null) {
|
|
||||||
int lastSlash = path.lastIndexOf('/');
|
|
||||||
if (lastSlash >= 0 && lastSlash < path.length() - 1) {
|
|
||||||
actualFileName = path.substring(lastSlash + 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (actualFileName == null || actualFileName.isEmpty()) {
|
|
||||||
actualFileName = "shared-image.jpg";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Store in SharedPreferences for plugin to read
|
// Store in SharedPreferences for plugin to read
|
||||||
storeSharedImageInPreferences(base64String, actualFileName);
|
storeSharedImageInPreferences(base64String, actualFileName);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user