简体   繁体   中英

How to get the image name when user upload the image from gallery and display them in textView?

  • I'm trying to retrieve the actual image name of the image selected by user in gallery(Ex. IMG_2020).
  • I tried using getAbsolutePath() and getName() but these 2 methods display something like "image$3A75" instead of the actual image file name.

Other than that, I also tried using some cursor method which I don't know really how it works, but it still doesn't work or maybe it's because I do not know how to use it.

Any help please?

This is my onActivityResult() method

  • fileName is the textView I want to place my imageName
  • bitMap object is not used yet because I'm following a tutorial online and I'm halfway copying, I think it will be used later in the tutorial

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();
            }
        }
    }
}

Something like:

                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();             

DATA not available on Android Q+.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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