Reading images from android device gallery

SOLVED[check post below]

Hi community,

Im looking for help overhere, currently im working on puzzle game. And right now im working on feature that allow’s user to load image from gallery and then i will generate puzzle from that same file. But i cant get it working, maybe someone of you could help me a bit.

This part of code [Written in Eclipse/Java], should get picked image path from gallery and send it to unity player. It’s working with this, but this doesn’t returns “real” image path:

It returns something like this: “content://media/external/images/media/4642”

public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if(resultCode == RESULT_OK)
        {
                Uri selectedImage = data.getData();
         
                UnityPlayer.UnitySendMessage("Main Camera", "OpenImage", selectedImage.toString());
        }
    }

I found this solution below and it seems that it should be working, but it’s not. It just crash my game when i try to run it:

public void openGallery()
    {
        Intent intent = new Intent(Intent.ACTION_PICK,
        android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);                 
        startActivityForResult(intent, 1);
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if(resultCode == RESULT_OK)
        {
            Uri selectedImage = data.getData();
     
            UnityPlayer.UnitySendMessage("Main Camera", "OpenImage", getRealPathFromURI(selectedImage));
        }
    }

    public String getRealPathFromURI(Uri contentUri) {

        // can post image
        String [] proj={MediaStore.Images.Media.DATA};
        Cursor cursor = managedQuery( contentUri,
                        proj, // Which columns to return
                        null,       // WHERE clause; which rows to return (all rows)
                        null,       // WHERE clause selection arguments (none)
                        null); // Order-by clause (ascending by name)
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();

        return cursor.getString(column_index);
}

I tried a lot of other solutions but all of them just crash the game. I tried to load image to Bitmap and then convert bitmap to string and send it to unity player. But it always crash, and i think it’s because i’m not getting the “real” path for image. Any ideas?

Ok, this is working perfectly.

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
     
        if(resultCode == RESULT_OK)
        {
            Uri selectedImage = data.getData();
         
            File image = new File(selectedImage.getPath());
         
            String s = getRealPathFromURI(selectedImage);
  
            UnityPlayer.UnitySendMessage("Main Camera", "OpenImage", "file://" + s);
        }
    }
 
    private String getRealPathFromURI(Uri contentURI) {
        Cursor cursor = getContentResolver().query(contentURI, null, null, null, null);
        if (cursor == null) { // Source is Dropbox or other similar local file path
            return contentURI.getPath();
        } else {
            cursor.moveToFirst();
            int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
            return cursor.getString(idx);
        }
    }
1 Like

Can you explain the code please?
I’m new in c#. I just copied and pasted the code above in new C#script. But there are many errors.

Code above is not C#, its Java they look similiar thats why it confused you.
https://docs.unity3d.com/Manual/PluginsForAndroid.html