繁体   English   中英

获取从android库中选择的图像的图像名称

[英]getting image name of image selected from gallery in android

我已经浏览了图库中的图像并将其设置为ImageView现在我需要获取已在ImageView设置的图像的图像名称。 我在下面附上了我的代码。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Log.d("req","req"+requestCode);
    Log.d("res","res"+resultCode);

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };
        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();
        imageView = (ImageView) findViewById(R.id.imgView);

        imageView1 = (ImageView) findViewById(R.id.imgView1);

        if(browse == 1){
            imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
        }
        if(browse == 2){
            imageView1.setImageBitmap(BitmapFactory.decodeFile(picturePath));
        }
        String resName = getResources().getResourceEntryName(R.id.imgView);
        Toast.makeText(getApplicationContext(), "BBB"+resName,Toast.LENGTH_LONG).show();

    }

试试这个如何从 URI 获取文件名

File f = new File(picturePath);
String imageName = f.getName();

根据android 文档

    /*
     * Get the file's content URI from the incoming Intent,
     * then query the server app to get the file's display name
     * and size.
     */
    Uri returnUri = returnIntent.getData();
    Cursor returnCursor =
            getContentResolver().query(returnUri, null, null, null, null);
    /*
     * Get the column indexes of the data in the Cursor,
     * move to the first row in the Cursor, get the data,
     * and display it.
     */
    int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
    int sizeIndex = returnCursor.getColumnIndex(OpenableColumns.SIZE);
    returnCursor.moveToFirst();
    TextView nameView = (TextView) findViewById(R.id.filename_text);
    TextView sizeView = (TextView) findViewById(R.id.filesize_text);
    nameView.setText(returnCursor.getString(nameIndex));
    sizeView.setText(Long.toString(returnCursor.getLong(sizeIndex)));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM