Skip to content

Commit

Permalink
Merge pull request #61 from shiv19/patch-1
Browse files Browse the repository at this point in the history
feat: added support for content:// paths on Android
  • Loading branch information
ryaa authored Sep 3, 2024
2 parents e23d310 + b930e85 commit 8f20fdc
Showing 1 changed file with 66 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import com.getcapacitor.PluginCall;
import com.getcapacitor.PluginMethod;
import com.getcapacitor.annotation.CapacitorPlugin;
import android.database.Cursor;
import android.content.ContentResolver;

import org.json.JSONObject;

Expand All @@ -24,49 +26,89 @@ public void open(PluginCall call) {
boolean openWithDefault = call.getBoolean("openWithDefault", true);

String fileName = "";
Uri fileUri = null;
try {
Uri fileUri = Uri.parse(filePath);
fileUri = Uri.parse(filePath);
fileName = fileUri.getPath();
} catch (Exception e) {
fileName = filePath;
}
File file = new File(fileName);
if (file.exists()) {
try {
if (contentType == null || contentType.trim().equals("")) {
contentType = getMimeType(fileName);
}
Intent intent = new Intent(Intent.ACTION_VIEW);
Context context = getActivity().getApplicationContext();
Uri path = FileProvider.getUriForFile(context, getActivity().getPackageName() + ".file.opener.provider", file);
intent.setDataAndType(path, contentType);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

if (openWithDefault) {
getActivity().startActivity(intent);
if (filePath.startsWith("content://")) {
ContentResolver contentResolver = getActivity().getContentResolver();
try (Cursor cursor = contentResolver.query(fileUri, null, null, null, null)) {
if (cursor != null && cursor.getCount() > 0) {
if (contentType == null || contentType.trim().equals("")) {
contentType = getMimeType(fileUri);
}
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(fileUri, contentType);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

if (openWithDefault) {
getActivity().startActivity(intent);
} else {
getActivity().startActivity(Intent.createChooser(intent, "Open File in..."));
}
call.resolve();
} else {
getActivity().startActivity(Intent.createChooser(intent, "Open File in..."));
call.reject("File not found", "9");
}
call.resolve();
} catch (android.content.ActivityNotFoundException exception) {
call.reject("Activity not found: " + exception.getMessage(), "8", exception);
} catch (Exception exception) {
call.reject(exception.getLocalizedMessage(), "1", exception);
}
} else {
call.reject("File not found", "9");
File file = new File(fileName);
if (file.exists()) {
try {
if (contentType == null || contentType.trim().equals("")) {
contentType = getMimeType(fileName);
}
Intent intent = new Intent(Intent.ACTION_VIEW);
Context context = getActivity().getApplicationContext();
Uri path = FileProvider.getUriForFile(context, getActivity().getPackageName() + ".file.opener.provider", file);
intent.setDataAndType(path, contentType);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

if (openWithDefault) {
getActivity().startActivity(intent);
} else {
getActivity().startActivity(Intent.createChooser(intent, "Open File in..."));
}
call.resolve();
} catch (android.content.ActivityNotFoundException exception) {
call.reject("Activity not found: " + exception.getMessage(), "8", exception);
} catch (Exception exception) {
call.reject(exception.getLocalizedMessage(), "1", exception);
}
} else {
call.reject("File not found", "9");
}
}
}

private String getMimeType(String url) {
String mimeType = "*/*";
int extensionIndex = url.lastIndexOf('.');
if (extensionIndex > 0) {
String extMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(url.substring(extensionIndex + 1));
if (extMimeType != null) {
mimeType = extMimeType;
String type = null;
String extension = MimeTypeMap.getFileExtensionFromUrl(url);
if (extension != null) {
type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
}
return type;
}

private String getMimeType(Uri uri) {
String type = null;
if (uri.getScheme().equals(ContentResolver.SCHEME_CONTENT)) {
ContentResolver cr = getActivity().getContentResolver();
type = cr.getType(uri);
} else {
String extension = MimeTypeMap.getFileExtensionFromUrl(uri.toString());
if (extension != null) {
type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
}
}
return mimeType;
return type;
}
}

0 comments on commit 8f20fdc

Please sign in to comment.