Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a piece of code that gets the path of the selected image from the gallery. now i want to get just the name of the image instead of the path. how can i do it,
the code that i used for getting the path is

Java
protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == PICK_IMAGE)
        {
            Uri imageUri = data.getData();
            imageView.setImageURI(imageUri);
        }
        if (resultCode == RESULT_OK) {

            Uri selectedImageUri = data.getData();
            File f = new File("" + selectedImageUri);
            String s = getRealPathFromURI(selectedImageUri);
            f.getName();

            TextView tv= (TextView) findViewById(R.id.textView);
            tv.setText(s);
        }
    }

    public String getRealPathFromURI(Uri uri) {
        String[] projection = { MediaStore.Images.Media.DATA };
        @SuppressWarnings("deprecation")
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);

    }
Posted
Comments
Rahul Ramakrishnan 14-Jul-15 2:45am    
i modified my code little bit but now i am getting the value as ACTUAL
this is the modified code

protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == PICK_IMAGE)
{
Uri imageUri = data.getData();
imageView.setImageURI(imageUri);
}
if (resultCode == RESULT_OK) {

Uri selectedImageUri = data.getData();
File f = new File("" + selectedImageUri);
//String s = getRealPathFromURI(selectedImageUri);
String s=f.getName();
//f.getName();

TextView tv= (TextView) findViewById(R.id.textView);
tv.setText(s);

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900