Meine App kann Fotos aus der Bibliothek auswählen. Genau ich möchte Dateipfad aus dieser Auswahl.
Dies ist der Code zum Erstellen der Absicht zum Auswählen eines Fotos:
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, INTENT_REQUEST_CODE_SELECT_PHOTO);
Dies ist der Code, der den Dateipfad von der URI erhält:
Cursor cursor = null;
String path = null;
try {
String[] projection = { MediaStore.Images.Media.DATA };
cursor = context.getContentResolver().query(contentUri, projection, null, null, null);
int columnIndex = cursor.getColumnIndexOrThrow(projection[0]);
cursor.moveToFirst();
path = cursor.getString(columnIndex);
} finally {
if (cursor != null) {
cursor.close();
}
}
return path;
Vor dem gestrigen Update der Google Photos App hat alles einwandfrei funktioniert. Jetzt ist path
nach dem Analysieren der URI null.
URI ist ähnlich wie folgt: content://com.google.Android.apps.photos.contentprovider/0/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F75209/ACTUAL
Ich habe auch versucht, Absichten mit der Aktion Intent.ACTION_GET_CONTENT zu erstellen - kein Glück.
Der folgende Code funktioniert für mich, um die Inhalts-URI auch für die neuesten Google Fotos abzurufen. Ich habe versucht, in temporäre Datei zu schreiben und temporäre Bild-URI zurückzugeben, wenn sie Berechtigung in der Inhalts-URI hat.
Sie können das Gleiche versuchen:
private static String getImageUrlWithAuthority(Context context, Uri uri)
{
InputStream is = null;
if (uri.getAuthority() != null)
{
try
{
is = context.getContentResolver().openInputStream(uri);
Bitmap bmp = BitmapFactory.decodeStream(is);
return writeToTempImageAndGetPathUri(context, bmp).toString();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
finally
{
try
{
if (is != null)
{
is.close();
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
return null;
}
private static Uri writeToTempImageAndGetPathUri(Context inContext, Bitmap inImage)
{
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}
Dies ist mit Sicherheit eine Problemumgehung, aber Sie können die echte Inhalts-URI extrahieren, die anscheinend aus irgendeinem Grund eingebettet wurde: content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F75209
Ich konnte eine neue URI mit authority=media and path=external/images/media/xxx
Erstellen und der Content Resolver gab eine echte URL zurück.
Beispielcode:
String unusablePath = contentUri.getPath();
int startIndex = unusablePath.indexOf("external/");
int endIndex = unusablePath.indexOf("/ACTUAL");
String embeddedPath = unusablePath.substring(startIndex, endIndex);
Uri.Builder builder = contentUri.buildUpon();
builder.path(embeddedPath);
builder.authority("media");
Uri newUri = builder.build();