Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Reading images from android device gallery

Discussion in 'Scripting' started by Marceta, Jan 8, 2015.

  1. Marceta

    Marceta

    Joined:
    Aug 5, 2013
    Posts:
    177
    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"

    Code (CSharp):
    1. public void onActivityResult(int requestCode, int resultCode, Intent data)
    2.     {
    3.         if(resultCode == RESULT_OK)
    4.         {
    5.                 Uri selectedImage = data.getData();
    6.          
    7.                 UnityPlayer.UnitySendMessage("Main Camera", "OpenImage", selectedImage.toString());
    8.         }
    9.     }
    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:

    Code (CSharp):
    1. public void openGallery()
    2.     {
    3.         Intent intent = new Intent(Intent.ACTION_PICK,
    4.         android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);                
    5.         startActivityForResult(intent, 1);
    6.     }
    7.  
    8.     public void onActivityResult(int requestCode, int resultCode, Intent data)
    9.     {
    10.         if(resultCode == RESULT_OK)
    11.         {
    12.             Uri selectedImage = data.getData();
    13.      
    14.             UnityPlayer.UnitySendMessage("Main Camera", "OpenImage", getRealPathFromURI(selectedImage));
    15.         }
    16.     }
    17.  
    18.     public String getRealPathFromURI(Uri contentUri) {
    19.  
    20.         // can post image
    21.         String [] proj={MediaStore.Images.Media.DATA};
    22.         Cursor cursor = managedQuery( contentUri,
    23.                         proj, // Which columns to return
    24.                         null,       // WHERE clause; which rows to return (all rows)
    25.                         null,       // WHERE clause selection arguments (none)
    26.                         null); // Order-by clause (ascending by name)
    27.         int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    28.         cursor.moveToFirst();
    29.  
    30.         return cursor.getString(column_index);
    31. }
    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?
     
    Last edited: Jan 8, 2015
  2. Marceta

    Marceta

    Joined:
    Aug 5, 2013
    Posts:
    177
    Ok, this is working perfectly.

    Code (CSharp):
    1.     @Override
    2.     public void onActivityResult(int requestCode, int resultCode, Intent data)
    3.     {
    4.         super.onActivityResult(requestCode, resultCode, data);
    5.      
    6.         if(resultCode == RESULT_OK)
    7.         {
    8.             Uri selectedImage = data.getData();
    9.          
    10.             File image = new File(selectedImage.getPath());
    11.          
    12.             String s = getRealPathFromURI(selectedImage);
    13.  
    14.             UnityPlayer.UnitySendMessage("Main Camera", "OpenImage", "file://" + s);
    15.         }
    16.     }
    17.  
    18.     private String getRealPathFromURI(Uri contentURI) {
    19.         Cursor cursor = getContentResolver().query(contentURI, null, null, null, null);
    20.         if (cursor == null) { // Source is Dropbox or other similar local file path
    21.             return contentURI.getPath();
    22.         } else {
    23.             cursor.moveToFirst();
    24.             int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
    25.             return cursor.getString(idx);
    26.         }
    27.     }
     
    Last edited: Jan 8, 2015
    quichelabs likes this.
  3. IslMav

    IslMav

    Joined:
    Jul 19, 2016
    Posts:
    1
    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.
     
  4. Marceta

    Marceta

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