Android 从库中的选定图像获取图像名称
目前,我正在从gallery中选择一幅图像,整个路径在textview中显示为示例(/storage/emulated.0/DCIM/Camera/123.jpg),而我只希望图像的名称显示为示例123.jpgAndroid 从库中的选定图像获取图像名称,android,Android,目前,我正在从gallery中选择一幅图像,整个路径在textview中显示为示例(/storage/emulated.0/DCIM/Camera/123.jpg),而我只希望图像的名称显示为示例123.jpg public String getRealPathFromURI(Uri uri) { String[] projection = { MediaStore.Images.Media.DATA }; @SuppressWarnings("deprecation")
public String getRealPathFromURI(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
@SuppressWarnings("deprecation")
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
BitmapFactory.Options options;
if (resultCode == RESULT_OK && requestCode == PICTURE_SELECTED) {
try {
options = new BitmapFactory.Options();
options.inSampleSize = 4;
Uri selectedImage = data.getData();
String s= getRealPathFromURI(selectedImage);
imageName.append(s);
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
String[] filePath = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePath, null, null, null);
cursor.moveToFirst();
String mImagePath = cursor.getString(cursor.getColumnIndex(filePath[0]));
InputStream stream = getContentResolver().openInputStream(selectedImage);
Bitmap yourSelectedImage = BitmapFactory.decodeStream(stream, null, options);
stream.close();
//---orientation---
try {
int rotate = 0;
try {
ExifInterface exif = new ExifInterface(
mImagePath);
dateTaken.append(exif.getAttribute(ExifInterface.TAG_DATETIME));
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_270:
rotate = 270;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotate = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_90:
rotate = 90;
break;
}
} catch (Exception e) {
e.printStackTrace();
}
Matrix matrix = new Matrix();
matrix.postRotate(rotate);
yourSelectedImage = Bitmap.createBitmap(yourSelectedImage , 0, 0, yourSelectedImage.getWidth(), yourSelectedImage.getHeight(), matrix, true);
}
catch (Exception e) {}
//---end of orientation---
imgButton.setScaleType(ImageView.ScaleType.FIT_CENTER);
imgButton.setImageBitmap(yourSelectedImage);
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Could not open file.", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(getApplicationContext(), "Image was not selected", Toast.LENGTH_LONG).show();
}
Log.i(TAG, "onActivityResult - Portal");
}
按照此链接,您需要执行字符串操作。下面的代码将帮助您完成此操作
String path=":/storage/sdcard0/DCIM/Camera/1414240995236.jpg";
String filename=path.substring(path.lastIndexOf("/")+1);
可能的副本: