android中图库的图像名称

时间:2016-04-05 09:55:36

标签: android

我正在使用以下代码行来获取图库中的图像路径 现在,我如何从画廊中选择该图像的名称?

 public void selectImage() {          
             new AlertDialog.Builder(getActivity()).setTitle("Select Picture")
                    .setAdapter(adapter, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int item) {
                            Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                            startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);
                                }
                            }
                        }
                    }).show();
        }  

        @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);    
            if (requestCode == PICK_IMAGE && resultCode == getActivity().RESULT_OK && null != data) {
                Uri selectedImageUri = data.getData();
                String selectedImagePath = getRealPathFromURIForGallery(selectedImageUri);                
            } 
        }

        public String getRealPathFromURIForGallery(Uri uri) {
            if (uri == null) {
                return null;
            }
            String[] projection = {MediaStore.Images.Media.DATA};
            Cursor cursor = getActivity().getContentResolver().query(uri, projection, null, null, null);
            if (cursor != null) {
                int column_index = cursor
                        .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                cursor.moveToFirst();
                return cursor.getString(column_index);
            }
            return uri.getPath();
        }

0 个答案:

没有答案