I want to pass the static images as uri so that whatsapp can use it as stickers.
I tried searching about React Native Images but all are talking about displaying the images.
asset resolver of Image component gives dyanamic URI like 'localhost:8080..assets/src/image/myImgae.webp' which is not helping to solve the issue.
let uri = Image.resolveAssetSource(require(src))
Although RNFS solves issue in alternate way. I have to use RNFS.copyAssetFile(asset uri, local storage path) and then use the local destination path to send it to whatsapp intent.
But accessing static image normally would be more reliable. Which is straight forward in android-java/kotlin .
Thanks
I am working on an offline app where I want to open and make changes to an offline document.
I am able to open the file from the android uri path i.e. /data/user/0/com.nativetemplate/files/files/default/documents/3377699720528075#1634821435640
but my requirement is to get the content:// uri path so I can save the changes made on the file.
eg: content://com.google.android.apps.docs.storage/document/acc%3D1%3Bdoc%3Dencoded%3D3ANW16SOSAt4EvkKHsYo_159-fKCSyFbzE4zHcCNeew3N7oDXd0jgvbNFDM%3D
how do I get the content uri?
My requirement can also work if the file is converted into base64. But as I have android uri path I think I would need an API to read the file from the uri.
please provide me with any suggestions you may have.
Thanks
We have some old issues with similar words, but most of them are about converting one or the other.
What I'm looking here is the "Right" behaviour of URI usage with the new changes. Let me give some context:
Before when we get an image URI this would return file://... format.
But since the new OS permissions changes, where we should not use WRITE_EXTERNAL_STORAGE anymore we should use getUriForFile(..) that return content://... path.(Scope Storage usage Android 11 Storage FAQ)
This can be spot on some Android guides, like: taken photos guide
The "problem" is that many users got used to use the URI of a crop image (for example) to create a file of it and save it.
Now, with this changes come the question:
How should we use the URI?
Make some code to check Android version and if more than 29 we should create a new file path for the URI?
Let the URI be the path to the image (content of file) and if someone wanna save it would need to create it own file path
Something else that I don't get yet about how to use URI right.
Obs: Asking this, because of a Android Image Crop open source project handover, where we need to upgrade the permissions for Android 10/11 but now we have this content/file issue. More here
Edit:
As pointed on the comments
Code returning file:// (not valid anymore since the changes)
Uri outputFileUri = null;
outputFileUri = Uri.fromFile(
new File(context.getExternalCacheDir().getPath(),
"pickImageResult.jpeg")
);
Code returning content://
outputFileUri = FileProvider.getUriForFile(
context,
context.getPackageName() + CommonValues.authority,
File.createTempFile("pickImageResult", ".jpeg", getImage)
);
The "problem" is that many users got used to use the URI of a crop image (for example) to create a file of it and save it.
In the end, this is your library, and you need to document what any Uri that you return is suitable for. After all, a Uri could point to:
A file on the filesystem (file)
A Web resource (https, or possibly http)
An Android resource (android.resource)
An asset in the app (file://android_asset)
Some arbitrary set of bytes (content)
Your library is for image cropping. While I have not examined the implementation, I assume that it all works inside the app itself. If so, there is nothing wrong with returning a file Uri, if you want to do so. Your code is writing a file somewhere (e.g., getCacheDir() on Context). The app using your library must have access to that file, or else you would have crashed trying to write it. A Uri created via Uri.fromFile(), for that file, is perfectly fine... in that app.
Where Uri.fromFile() becomes a problem is in passing the Uri to another app. However, your library is for cropping images, not sharing content with other apps. Your job, IMHO, is to give a cropped image back to the app. What the app does with it is up to that app, subject to whatever limitations there are in the Uri that you hand over.
The two options that you seem to be considering have different issues:
Uri Source
Advantages
Disadvantages
Uri.fromFile()
Cheap, easy
Can only be used within the app itself; cannot be passed to other apps
FileProvider
Uri can be passed to other apps
Requires a library and manifest configuration; cannot readily get to the underlying file
Since IMHO an image cropper is not an image sharing solution, Uri.fromFile() seems reasonable. If the app using your library wants to turn around and share the cropped image, they would set up FileProvider themselves and use FileProvider.getUriForFile(). The only catch is that either you need to document where the file will be written or give them an option to tell you what directory to use — that information will be needed to set up the FileProvider metadata.
Someday, if you elect to change the API, you might consider returning an ordinary File instead of a Uri. That way, there is no confusion about what it represents.
But, in the end, this is all your decision. If you want to use FileProvider, or you want to upload images to your own Web server and use https, that is all up to you. However, you should document what you are doing and what the Uri represents.
I'm working on the Android version of an app I did for iOS. I have some files (could be PDF, DOC, PNG, etc) in cloud storage and I retrieve them using an ASPX handler (like http://www.myserver.com/GetFile?name=test.png).
The goal is to DISPLAY ONLY, not download the files.
In iOS I used a UIWebView and it would download and preview the file as long as the OS could handle that file type.
What is the best approach for Android? I've tried the following:
WebView and embedding in google docs (http://docs.google.com/gview?embedded=true&url=) - DOES NOT WORK FOR PNG
Launching a new Activity using myIntent.setDataAndType(myFileURI, myFileMIME); where myFileMIME = MimeTypeMap.getSingleton().getMimeTypeFromExtension(myFileTypeExtension). For PNGS I get "No activity for type image/png"
I think you can try DownloadManager for this.
The second option is a better way for Android, after fetching the file from cloud.
Use MimeType as "image/*", which usually works for all images and opens in default gallery.
So what I've ended up doing is classifying the MIME type as shown above. If the type is "image/*" then I display the image directly in a WebView.
If the type is "application/*" then I embed it in google docs using the 1st method, and display it in the WebView.
Still looking for a better solution.
I got some .mp4 files in my /raw folder which i want to share with the Android Share. The following code works with Facebook Messenger (the video is sended) but not with other applications like WhatsApp, Gmail, Facebook, etc.
I got the following code:
String path = "android.resource://com.my.package/raw/" + "name_mp4";
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("video/mp4");
share.putExtra(Intent.EXTRA_STREAM,Uri.parse(path) );
startActivity(share);
What am I doing wrong? Why does it nog work for the other Apps?
What am I doing wrong?
Few apps will know what to do with an android.resource Uri. The ones that will mostly will be ones passing it straight to openInputStream() on a ContentResolver. That scheme is very rarely used.
If you want to improve compatibility, use a content:// or file:// Uri, pointing at a ContentProvider or file on external storage, respectively.