如何从图库中获取图像,获取图像名称并将其保存在内部存储器中并获取该图像的路径
问题描述:
(我很抱歉,如果我用英文书写错误) 我需要按照标题说 我只知道如何从画廊获得图像。我认为这是正确的:如何从图库中获取图像,获取图像名称并将其保存在内部存储器中并获取该图像的路径
(如果是错误的,或者它是无效的,请让我知道)
private final int SELECT_PICTURE = 200;
btnAddImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent galeryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
galeryIntent.setType("image/*");
startActivityForResult(galeryIntent.createChooser(galeryIntent, "Sececiona Imagen"), SELECT_PICTURE);
}
});
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode){
case (SELECT_PICTURE):
if(requestCode == RESULT_OK){
Uri path = data.getData();
}
}
break;
}
}
但我不知道怎么弄,我从捕捉图像的名称并在我的应用程序的内部存储器中保存该名称。 然后我需要像“MyAPP/media/...”这样的图像路径,我不知道英文的正确名称,并将其保存在我的数据库中(保存在我的数据库中很容易),然后在我的项目中使用它
我知道这是一个很长的消息,但我需要你的帮助,因为在*的西班牙论坛有人回答我。
答
onActivityResult:
String path = getPath(getApplicationContext(), data.getData());
File mFile = new File(id);
mFile.getName();
这是一个得到了文件名的方式。
而且的getPath:
public static String getPath(final Context context, final Uri uri) {
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
if (isExternalStorageDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
Log.d("Lucas","TYPE => "+type);
if ("primary".equalsIgnoreCase(type)) {
return Environment.getExternalStorageDirectory() + "/" + split[1];
}
} else if (isDownloadsDocument(uri)) {
final String id = DocumentsContract.getDocumentId(uri);
final Uri contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
return getDataColumn(context, contentUri, null, null);
} else if (isMediaDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
Uri contentUri = null;
if ("image".equals(type)) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if ("video".equals(type)) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if ("audio".equals(type)) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
final String selection = "_id=?";
final String[] selectionArgs = new String[] {
split[1]
};
return getDataColumn(context, contentUri, selection, selectionArgs);
}
} else if ("content".equalsIgnoreCase(uri.getScheme())) {
return getDataColumn(context, uri, null, null);
} else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
return null;
}
public static String getDataColumn(Context context, Uri uri, String selection,
String[] selectionArgs) {
Cursor cursor = null;
final String column = "_data";
final String[] projection = {
column
};
try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
null);
if (cursor != null && cursor.moveToFirst()) {
final int column_index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(column_index);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}
public static boolean isExternalStorageDocument(Uri uri) {
return "com.android.externalstorage.documents".equals(uri.getAuthority());
}
public static boolean isDownloadsDocument(Uri uri) {
return "com.android.providers.downloads.documents".equals(uri.getAuthority());
}
public static boolean isMediaDocument(Uri uri) {
return "com.android.providers.media.documents".equals(uri.getAuthority());
}
@Isca Android的说:无法解析方法的getPath() – ProRiderZ115
@ ProRiderZ115对不起,这里是的getPath方法:) – lsca
@Isca无法解析建立和isDocumentUri和getDocumentID中下划线红色 (很多thx为你的帮助) – ProRiderZ115