0

I want to to get the image name while getting the image from gallery . How to do that? Below is my code. I have no idea how to proceed.

            public void onClick(View v) {
                Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(i, IMAGE_PICKER_SELECT);
            }
        });
        return v;
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == IMAGE_PICKER_SELECT  && resultCode == Activity.RESULT_OK) {
            bitmap = getBitmapFromCameraData(data, mainActivity);
            bitmap = getBitmapFromCameraData(data, mainActivity);
            profileImageView.setImageBitmap(bitmap);
            uploadImage();
        }
    }

    private Bitmap getBitmapFromCameraData(Intent data, Context context) {


        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };
        Cursor cursor = context.getContentResolver().query(selectedImage,filePathColumn, null, null, null);
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();
        return BitmapFactory.decodeFile(picturePath);



    }
  • But you have picturePath. There is name. – thealeksandr Jun 24 '16 at 10:18
  • @stackover65 log your picturePath you will see it – Nisarg Jun 24 '16 at 10:18
  • 1
    Can you elaborate on how your code "doesn't work"? What were you expecting, and what actually happened? If you got an exception/error, post the line it occurred on and the exception/error details. Please [edit] these details in or we may not be able to help. – Claudia Jun 24 '16 at 10:41

2 Answers2

-1

Following code will help you to get the image name of selected file:

private String getFileName(Uri selectedImage){
 File f = new File(picturePath);
 String imageName = f.getName();
 return imageName;
}

Happy Coding !!!

Anjali
  • 1
  • 1
  • 13
  • 20
-1

the picturepath which you are getting comprises of name of your image take it out from there. for example String path = cursor.getString(coulmnIndex); String[] temp = path.split("/"); String name_of_image = temp[temp.length];

Ashwani
  • 1,294
  • 1
  • 11
  • 24