【问题标题】:How to get the image name when user upload the image from gallery and display them in textView?用户从图库中上传图像并在 textView 中显示时如何获取图像名称?
【发布时间】:2020-06-09 00:27:40
【问题描述】:
  • 我正在尝试检索用户在图库中选择的图像的实际图像名称(例如 IMG_2020)。
  • 我尝试使用 getAbsolutePath() 和 getName() 但这两种方法会显示类似“image$3A75”的内容,而不是实际的图像文件名。

除此之外,我还尝试使用一些我不知道它是如何工作的光标方法,但它仍然不起作用或者可能是因为我不知道如何使用它。

有什么帮助吗?

这是我的 onActivityResult() 方法

  • fileName 是我要放置 imageName 的 textView
  • bitMap对象还没用,因为我在网上跟着一个教程,正在复制一半,我想以后教程会用到

public class createCharity extends AppCompatActivity {

    private static final int PICK_IMAGE_REQUEST = 1;
    Button uploadButton;
    Button createCharityButton;
    TextView charityTitle;
    TextView fileName;
    TextView charityDescription;
    Uri charityImage;
    private StorageReference mStorageRef;
    private DatabaseReference mDatabaseRef;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_charity);

        uploadButton = findViewById(R.id.uploadButton);
        charityTitle = findViewById(R.id.charityTitle);
        fileName = findViewById(R.id.fileName);
        charityDescription = findViewById(R.id.charityDescription);
        createCharityButton = findViewById(R.id.createCharityButton);

        mStorageRef = FirebaseStorage.getInstance().getReference("charityUploads");
        mDatabaseRef = FirebaseDatabase.getInstance().getReference("charityUploads");

        uploadButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openFileChooser();
            }
        });

    }

    private void openFileChooser() {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(intent, PICK_IMAGE_REQUEST);
        System.out.println("----------------------c1");
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
            charityImage = data.getData();
            try {
                Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), charityImage);

                File file = new File(String.valueOf(charityImage));
                System.out.println("Image name: " + file.getName());

                fileName.setText(file.getName());

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

【问题讨论】:

    标签: java android file directory path


    【解决方案1】:

    类似:

                    String projection [] = {
    
                              MediaStore.Images.Media.DATA
                            , MediaStore.Images.Media.DISPLAY_NAME
                            , MediaStore.Images.Media.SIZE};
                    Cursor cursor = getContentResolver().query(data.getData(), projection, null, null, null);
    
                    if ( cursor==null)
                    {
                        Toast.makeText(context, "cursor==null\n\ncould not query content resolver for\n\n" + path, Toast.LENGTH_LONG).show();
    
                        return;
                    }
    
                    cursor.moveToFirst();
                    String data = cursor.getString(0);
                    String displayName = cursor.getString(1);
                    String size = cursor.getString(2);
    
                    Toast.makeText(context, "getContentResolver().openInputStream() ok\n\n" + path
                                + "\n\nDISPLAY_NAME: " + displayName
                                + "\nDATA: " + data
                                + "\nSIZE: " + size
                                , Toast.LENGTH_LONG).show();
                    cursor.close();             
    

    数据在 Android Q+ 上不可用。

    【讨论】:

    • 非常感谢! T.T 我从数组中删除了 DATA 和 SIZE,因为我不需要它们,并对变量名、索引做了一些小的更改,它起作用了 :)
    猜你喜欢
    • 1970-01-01
    • 2016-11-16
    • 1970-01-01
    • 2016-07-18
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 2019-02-20
    • 2019-07-26
    相关资源
    最近更新 更多