Uploading file with the uri starting with content:// - android

I am reading a file from android and uploading it to a remote server.I am getting the file in the form of
content://com.android.providers.downloads.documents/document/3
Since i am using react-native, can the uri above used as a file path?.
I can get the exact file path but when the file picker gets the file and the file has spaces, the picker cannot read the file but the uri takes into account spaces.
What can be done to use content://com.android.providers.downloads.documents/document/3 as the file path?.

If i understand what you want , its something like this
var x = "content://com.android.providers.downloads.documents/document/3"
const file = {
uri: x,
name: "nameFile",
}
const formdata = new FormData();
formdata.append('Files', file);
fetch('URL', {
...

Related

Files to Base64 conversion using RNFS

Im trying to convert a file to base 64. for that im using react-native-document-picker and RNFS.
Problem: When files is picked thru
react-native-document-picker it gives me success and the path is temporary cache path. but when i try to convert it to base64 using RNFS it gives me that error.
it will pick file from iCloud but not which is showed as "On My iPhone" in file manager i think issue is its not pick images from local storage of iPhone.
ENOENT: no such file or directory, open '/private/var/mobile/Containers/Data/Application/A78E7E14-DBAC/tmp/com.xyz-Inbox/Scanned%20Document.pdf'
here's my code:
const response = await DocumentPicker.pickSingle({type: [DocumentPicker.types.allFiles],
copyTo: "documentDirectory" });
var data = await RNFS.readFile(response.uri, 'base64');
Thanks for help.

How to get actual file type if it is renamed to something different file extension in android?

I want to upload images on the server, unfortunately, my server supports only jpg/jpeg file formats. If I will upload a png file I will get an error. So before uploading to the server, I want to make sure the file type is only jpg/ jpeg. But I am afraid that some users can change a png file type to jpg just by renaming it. For eg- myfile.png can be renamed easily with myfile.jpg. I used below approach to check file type in Android/ Kotlin -
fun getMimeType(context: Context, uri: Uri): String {
var mimeType = ""
mimeType = if (uri.scheme.equals(ContentResolver.SCHEME_CONTENT)) {
val cr = context.contentResolver
cr.getType(uri) ?: ""
} else {
val fileExtension = MimeTypeMap.getFileExtensionFromUrl(uri
.toString())
MimeTypeMap.getSingleton().getMimeTypeFromExtension(
fileExtension.toLowerCase(Locale.getDefault())) ?: ""
}
return mimeType
}
But it returns only jpg/jpeg even in case of png file renamed to jpg. I think it's checking by file extension only. So my question is - Is there any way to check the original file type (in our case it is a png file)?
Happy Coding :-)
You have to check file's Header and verify if it respects JPEG formats (this procedure is valid for many type of files) https://wiki.fileformat.com/image/jpeg/

How to read from path to media file that located under raw dir (native)?

I have native JNI implementation and for now I have my media files under the app dir /storage/emulated/0/Android/data/com.my_app.debug/files/Models/my_media_file.mp4 in order to start read one of the files I need to pass a path to JNI where inside I use such method that expect to get path_to_mp4_file
bool Decoder::InitFromFile(std::string const &filename)
{
m_file.open(filename, std::ios::binary);
if (!m_file.is_open())
{
return false;
}
...
}
And all works, but now I need to read from build-in raw folder, so now in order to get path to my file I am using this way
fun foo()
{
...
val path = "android.resource://" + activity!!.packageName + "/" + R.raw.my_media_file
val uri = Uri.parse(path)
JNIImplementation.InitFromFile(uri.path)
...
}
But now uri = android.resource://com.my_app.debug/2131492865 if I get uri.path = /2131492865.
Issue is that if I pass this uri.path as a path to InitFromFile method I get an error, because I can't open file by path /2131492865.
So, as far as I understand there is should be a way to read from raw or assert folder native way or something like this
How to do it?
Or maybe there is a way how to put this media file in app (build-in) and have access to get path as to regular file? I mean somehow look at this file not as a resource, but as a simple file?

Android Doesn't Recognize Image Downloaded by Flutter App

Android Flutter app downloads an image from a server on another Android phone. After downloading, the file is unable to be opened or viewed by the phones gallery or file explorer. However, when examined by a hex editor, the downloaded file and the original file are exact copies, and when imported to windows, the "corrupt" downloaded file is view able by the Image Viewer. The only difference I could find between the files was the metadata examined by Windows. What could be the cause of this?
Original File on Android Server:
Downloaded File On Android Client:
Here's the code I'm using to create the file from a Uint8list:
Future<File> downloadFileAndroid(Uint8List fileBytes, String fileName) async{
var dir = await getExternalStorageDirectory();
File photoFile;
var photoDirectory = await Directory(dir.path + "/Downloader").create(recursive: true);
photoFile = await new File(photoDirectory.path + "/" + fileName).create();
if(await photoFile.exists()){
await photoFile.writeAsBytes(fileBytes);// also tried flush: true
print("Created file and it exists");
} else {
print("Error: tried to create file but it doesnt exist");
}
}
I faced this problem when downloading an image from the internet and setting a png extention to it. Using an extention on the file name usually causes this error. Consider using https://pub.dev/packages/image_downloader to download the image using the default function in the example code. With the default option, the extension is not determined by the coder but instead by itself.

Import PDF in my xamarin shared code application

I want to import pdf in my xamarin android project.
I want to have a pdf in my application folder at the installation of the application.
Bu when i try to add this pdf in android ressources and try to open it, i get a path error.
i save my pdf in draxable ressource and call path like : #drawable/pdfname or pdfname. the two solution don t work.
Best regards,
ok, i put it in the asset folder. I want to send email with attachement. I try this but the app said me the file is empty. var file = new Java.IO.File("Assets/CV_2017.pdf"); var uri = Android.Net.Uri.FromFile(file); i set my ressource as "build as android asset".
To get the url of the file in assets folder, you can code for example like this:
var file = new File(Android.Net.Uri.Parse("file:///android_asset/CV_2017.pdf").ToString());
if (file.Exists())
{
var uri = file.AbsolutePath;
}
If you want to access the stream of the file under assets, you can use AssetManager and code for example like this:
AssetManager assets = this.Assets;
using (StreamReader sr = new StreamReader (assets.Open ("CV_2017.pdf")))
{
//TODO:
}

Categories

Resources