I used nativescript-imagepicker (related website) and I implement all the things exactly as were in the document and sample codes.
I also set the permission code in the AndroidManifest.xml, that fixed for the API 29 and upper, but unfortunately I get this error after choosing a photo from the device gallery:
Asset 'content://com.android.providers.downloads.documents/document/..._photos.jpg' cannot be found.
Code:
let context = imagepicker.create({
mode: "single"
});
context.authorize()
.then(() => {
return context.present();
}).then(selection => {
const imageAsset = selection.length > 0 ? selection[0] : null;
imageAsset.options.height = 1024;
imageAsset.options.width = 1024;
imageAsset.options.keepAspectRatio = true;
ImageSource.fromAsset(imageAsset).then((imageSource: ImageSource) => {
this.defaultImage = "";
this.changedImage = imageSource;
this.uploadImage(imageSource);
}, (err) => {
console.log(err);
})
})
.catch(function (e) {
console.log(e);
});
AndroidManifest.xml:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application android:requestLegacyExternalStorage="true" ... >
PS: someone has written that adding compileSdkVersion = 29
to the app.gradle should fix it but that doesn't work!
Anyone can help please?
hi I found your answer after a long time on search.imagepicker return image address in content://
format so imagesource cant find out it.you must convert content://
format to file://
format.so change your code same below
let context = imagepicker.create({
mode: "single"
});
context.authorize()
.then(() => {
return context.present();
}).then(selection => {
const imageAsset = selection.length > 0 ? selection[0] : null;
imageAsset.options.height = 1024;
imageAsset.options.width = 1024;
imageAsset.options.keepAspectRatio = true;
const source = new ImageSource();
if (imageAsset.android) {
//console.log(getPathFromUri(imageAsset.android));
ImageSource.fromFile(getPathFromUri(imageAsset.android)).then((imageSource: ImageSource) => {
this.defaultImage = "";
this.changedImage = imageSource;
this.uploadImage(imageSource);});
//viewModel.uploadFile(file);
}else{
source.fromAsset(imageAsset).then((imageSource: ImageSource) => {
this.defaultImage = "";
this.changedImage = imageSource;
this.uploadImage(imageSource);
})}
}).catch(function (e) {
console.log(e);
});
export function getPathFromUri(path) {
let context=Utils.android.getApplicationContext();
let uri = android.net.Uri.parse(path);
let isKitKat = android.os.Build.VERSION.SDK_INT >=
android.os.Build.VERSION_CODES.KITKAT;
// DocumentProvider
// if (isKitKat )
{
// ExternalStorageProvider
if (uri.getAuthority().includes("externalstorage")) {
let docId = android.provider.DocumentsContract.getDocumentId(uri);
let split = docId.split(":");
let type:string = split[0];
//System.out.println("getPath() docId: " + docId + ", split: " + split.length + ", type: " + type);
// This is for checking Main Memory
if ("primary"===type.toLocaleLowerCase()) {
if (split.length > 1) {
return android.os.Environment.getExternalStorageDirectory() + "/" + split[1] + "/";
} else {
return android.os.Environment.getExternalStorageDirectory() + "/";
}
// This is for checking SD Card
} else {
return "/storage/emulated/0" + "/" + docId.replace(":", "/");
}
}
}
return null;
}