Beim Zugriff auf das Bild über die Android-Galerie kann die Bildquelle nicht festgelegt werden
-
21-12-2019 - |
Frage
Ich verwende Cordova 3.4.Wenn ich ein Bild aufnehme und einstelle, funktioniert es einwandfrei, aber wenn ich versuche, auf ein Bild aus der Galerie zuzugreifen, erhalte ich die URL
content://com.android.providers.media.documents/document/image%4A463
und ich erhalte einen Bildladefehler in meinem Bild-Tag. Ich weiß, dass dies ein bekannter Fehler ist, und ich habe auf Stapelfragen verwiesen, z Bei Auswahl aus der Galerie auf Android 4.4 (KitKat) mit dem PhoneGap-Kamera-Plugin kann kein Bild geladen werden
Ich kann die erwähnte grobe Umgehungslösung, bei der der URI manuell festgelegt wird, wie content://media/external/images/media/, nicht verwenden, da wir uns auf Geräte mit internem integriertem Speicher wie Moto konzentrieren
Können Sie bitte auch bestätigen, dass dieses Problem in Cordova 3.5 behoben wurde? Ich benötige für den Download die Genehmigung meiner Beamten und muss sicherstellen, dass dies möglich ist. Kann mir bitte jemand helfen?
AKTUALISIEREN:
app.directive('upload', function() {
return {
restrict: 'A',
require: 'ngModel',
link: function(scope, elm, attrs, ctrl) {
elm.on('click', function() {
navigator.camera.getPicture(
function(imageURI) {
scope.$apply(function() {
alert(imageURI);
ctrl.$setViewValue(imageURI);
});
window.resolveLocalFileSystemURI(imageURI, function(fileEntry) {
// ctrl.$setViewValue(imageURI);
alert("inside local file" + imageURI);
alert("Inside local file system");
fileEntry.file(function(fileObj) {
alert("hai");
var options = new FileUploadOptions();
options.fileKey = "file";
options.fileName = fileObj.name;
options.mimeType = fileObj.type;
var ft = new FileTransfer();
// var url = fileUploadUrl;
// ft.upload(imageUri, encodeURI(url), success, failure, options);
});
});
},
function(err) {
ctrl.$setValidity('error', false);
}, {quality: 50,
destinationType: Camera.DestinationType.FILE_URI,
sourceType: Camera.PictureSourceType.PHOTOLIBRARY
}
);
});
}
};
});
Mein HTML:
<img
ng-src="{{onepic}}"
width="250"
height="390"
alt="error"/>
Ich habe in meinem Controller in der Watch-Funktion einen Wert für onepic festgelegt. Wenn die Direktive also einen URI zurückgibt, wird dieser automatisch in onepic festgelegt. Bitte geben Sie eine alternative Lösung an. Wenn ich etwas falsch mache, bin ich neu bei AngularJS
Lösung
Versuche dies..
Im Kamera-Plugin gibt es eines FileHelper.java
Datei, Ersetzen Sie das Plugin getRealPath
mit meiner das ist meine Methode.
Für weitere Details können Sie diesem folgen -> https://issues.apache.org/jira/browse/CB-5398
FileHelper.java
@SuppressWarnings("deprecation")
public static String getRealPath(String uriString, CordovaInterface cordova) {
String realPath = null;
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
// DocumentProvider
if (isKitKat) {
Cursor cursor = cordova.getActivity().getContentResolver().query(Uri.parse(uriString), null, null, null, null);
cursor.moveToFirst();
String document_id = cursor.getString(0);
document_id = document_id.substring(document_id.lastIndexOf(":")+1);
cursor.close();
cursor = cordova.getActivity().getContentResolver().query(
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
null, MediaStore.Images.Media._ID + " = ? ", new String[]{document_id}, null);
cursor.moveToFirst();
String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
realPath = path;
cursor.close();
}else{
if (uriString.startsWith("content://")) {
String[] proj = { _DATA };
Cursor cursor = cordova.getActivity().managedQuery(Uri.parse(uriString), proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(_DATA);
cursor.moveToFirst();
realPath = cursor.getString(column_index);
if (realPath == null) {
LOG.e(LOG_TAG, "Could get real path for URI string %s", uriString);
}
} else if (uriString.startsWith("file://")) {
realPath = uriString.substring(7);
if (realPath.startsWith("/android_asset/")) {
LOG.e(LOG_TAG, "Cannot get real path for URI string %s because it is a file:///android_asset/ URI.", uriString);
realPath = null;
}
} else {
realPath = uriString;
}
}
return realPath;
}
In CameraLauncher.java
Klasse gibt es eine Methode processResultFromGallery
private void processResultFromGallery
Finden Sie diesen Code:
if (this.targetHeight == -1 && this.targetWidth == -1 &&
(destType == FILE_URI || destType == NATIVE_URI) && !this.correctOrientation){
this.callbackContext.success(uri.toString());
}
Und ersetzen Sie es durch Folgendes:
if (this.targetHeight == -1 && this.targetWidth == -1 &&
(destType == FILE_URI || destType == NATIVE_URI) && !this.correctOrientation) {
String s =FileHelper.getRealPath(uri.toString(), cordova);
Log.i("test","<<<<ifURI::::::"+s +"URI::::" + uri.toString());
this.callbackContext.success(s);
}
Andere Tipps
siehe diese Antwort:
generasacodicetagpre.Wann immer einige uri
wird übergeben <img src="uri" />
es wird implizit dekodiert von
content://com.android.providers.media.documents/document/image%3A9888 (1)
hinein
content://com.android.providers.media.documents/document/image:9888 (2)
Allerdings nach der Rückkehr von Intent.ACTION_OPEN_DOCUMENT
oder Intent.ACTION_GET_CONTENT
Android stellt Ihnen die Leseberechtigung für zur Verfügung (1), nicht (2).In diesem Fall WebView
wird erwartungsgemäß einen Fehler protokollieren:
java.lang.SecurityException:Verweigerung der Erlaubnis:Lesen com.android.providers.media.mediadocumentsProvider uri content: //com.android.providers.media.documents/document/image: 9888 aus PID = 13163, uid = 10165 Fordert und Roid.Permission.Manage_Documents, oder Granturpermission () ()).
oder
Inhalts-URL kann nicht geöffnet werden
Code-Auszug
Alles, was Sie brauchen, um das Problem zu lösen, ist
String uriToUseInWebView = transformForWebView(uri.toString());
private String transformForWebView(String uri) {
for (int i = 0; i < timesDecodingWebView(); i++)
uri = uri.replace("%", Uri.encode("%"));
return uri;
}
private int timesDecodingWebView() {
if (Build.VERSION.RELEASE.equals("4.4.2")) {
return 2;
} else {
return 1;
}
}
in Ihrem Java-Code vor der Übergabe uri
in HTML/JS, um dies sicherzustellen (1) wird tatsächlich geladen.
Ich habe das auf 4.4.2, 4.4.4 und 5.0 getestet.Das Lustige daran ist, dass Android 4.4.2 das dekodiert uri
intern zweimal.