0

I'm not going to print out the entire stack trace cause it's huge, but essentially I'm trying to create a FileHandle for the file 'document/image:136635' and I'm getting no such file or directory.

com.badlogic.gdx.utils.GdxRuntimeException: Couldn't load file: /document/image:136635

Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)

So I'm using the path 'document/image:136635' because I launched an activity for the android gallery app so the user can select a photo of themselves for the avatar. The selection seems to works ok, we can select an image and my program continues afterwards, but they when I try to create a filehandle for the image so I can actually draw it in my game I have problems. When I analyse the data returned from the activity the path of the photo they selected comes back as 'document/image:136635'. Is that a valid path for an image ordinarily? If I use a file viewer on my phone I can see that there is a document folder but nothing is in it.

Any advice appreciated. Thanks.

edit: So the info in the URI is (in order):

image:136635

image/jpeg

IMG_20190702_175942.jpg

1562065182000

5

1285506

So I can get the actual filename, but still not the real path? I don't think...

user8810083
  • 599
  • 1
  • 5
  • 21

3 Answers3

1

try this code into your app its work fine

private static final int GALLERY_PICK = 1;


Intent galleryIntent = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(galleryIntent, PICK_IMAGE_REQUEST);


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && 
data.getData() != null) {

    Uri uri = data.getData();

    try {
        Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
        // Log.d(TAG, String.valueOf(bitmap));

        ImageView imageView = (ImageView) findViewById(R.id.imageView);
        imageView.setImageBitmap(bitmap);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
}
  • I'm using libgdx so will need to convert the bitmap to a Texture. I guess the quickest way would be convert the bitmap to a byte array and then convert the bytearray to a pixmap and then finally convert the pixmap to a texture? seem a long and roundabout way to do it. Ideally I just want the actual path of the image so I can create the texture directly from that and not have all these extra steps – user8810083 Jul 03 '19 at 06:48
  • Loading the Bitmap should be done on a background Thread, such as with an AysncTask. Otherwise you risk an ANR. – Tenfour04 Jul 03 '19 at 15:21
1
 private static final int GALLERY_PICK = 1;


Intent galleryIntent = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(galleryIntent, GALLERY_PICK);


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == GALLERY_PICK && resultCode == RESULT_OK && data != null && 
data.getData() != null) {

    Uri uri = data.getData();

    try {

     String filePath = getPathNew(this,uri);  //return actual path

    } catch (IOException e) {
        e.printStackTrace();
    }
}
}

To get Actual Path--

@SuppressLint("NewApi")
    public static String getPathNew(final Context context, final Uri uri) {

        final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;

        // DocumentProvider
        if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
            // ExternalStorageProvider
            if (isExternalStorageDocument(uri)) {
                final String docId = DocumentsContract.getDocumentId(uri);
                final String[] split = docId.split(":");
                final String type = split[0];

                if ("primary".equalsIgnoreCase(type)) {
                    return Environment.getExternalStorageDirectory() + "/" + split[1];
                }
                // TODO handle non-primary volumes
            }
            // DownloadsProvider
            else if (isDownloadsDocument(uri)) {

                final String id = DocumentsContract.getDocumentId(uri);
                final Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"),
                        Long.valueOf(id));
                return getDataColumn(context, contentUri, null, null);
            }
            // MediaProvider
            else if (isMediaDocument(uri)) {
                final String docId = DocumentsContract.getDocumentId(uri);
                final String[] split = docId.split(":");
                final String type = split[0];

                Uri contentUri = null;
                if ("image".equals(type)) {
                    contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                } else if ("video".equals(type)) {
                    contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
                } else if ("audio".equals(type)) {
                    contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
                }

                final String selection = "_id=?";
                final String[] selectionArgs = new String[]{split[1]};

                return getDataColumn(context, contentUri, selection, selectionArgs);
            }
        }
        // MediaStore (and general)
        else if ("content".equalsIgnoreCase(uri.getScheme())) {
            return getDataColumn(context, uri, null, null);
        }
        // File
        else if ("file".equalsIgnoreCase(uri.getScheme())) {
            return uri.getPath();
        }

        return null;
    }

    public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {

        Cursor cursor = null;
        final String column = "_data";
        final String[] projection = {column};

        try {
            cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
            if (cursor != null && cursor.moveToFirst()) {
                final int column_index = cursor.getColumnIndexOrThrow(column);
                return cursor.getString(column_index);
            }
        } finally {
            if (cursor != null)
                cursor.close();
        }
        return null;
    }

    /**
     * @param uri The Uri to check.
     * @return Whether the Uri authority is ExternalStorageProvider.
     */
    public static boolean isExternalStorageDocument(Uri uri) {
        return "com.android.externalstorage.documents".equals(uri.getAuthority());
    }

    /**
     * @param uri The Uri to check.
     * @return Whether the Uri authority is DownloadsProvider.
     */
    public static boolean isDownloadsDocument(Uri uri) {
        return "com.android.providers.downloads.documents".equals(uri.getAuthority());
    }

    /**
     * @param uri The Uri to check.
     * @return Whether the Uri authority is MediaProvider.
     */
    public static boolean isMediaDocument(Uri uri) {
        return "com.android.providers.media.documents".equals(uri.getAuthority());
    }

add file READ WRITE permissions in your manifest

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Prachi Singh
  • 564
  • 3
  • 8
  • unfortunately uri.getPath() does not seem to return the actual filepath. e.g. it returns /document/image:136635, when in actual fact the file is /pictures/screenshots/Screenshot_2019-07-02-27-34-789.jpeg. I need the actual filepath so I can create a libgdx texture – user8810083 Jul 03 '19 at 07:08
  • @user8810083 see my edited answer.. getPathNew() will give you actual path of image. – Prachi Singh Jul 03 '19 at 07:31
  • I think getting closer but now I'm getting permission denied with this new getpathnew method. I tried the solution in this post https://stackoverflow.com/a/33292700/8810083 but am having some difficulty getting ActivityCompat to work with libgdx – user8810083 Jul 03 '19 at 10:03
  • After api 23 that is not enough. You can see they explain in that thread – user8810083 Jul 03 '19 at 10:35
  • getDataColumn(context, uri, null, null); will return path after api 19 – Prachi Singh Jul 03 '19 at 10:46
  • I tried on API 28. Still permission error Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media from pid=5061, uid=10085 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission(). – user8810083 Jul 03 '19 at 11:06
  • got both of these in my android manifest : – user8810083 Jul 03 '19 at 11:07
  • ok this post seems to have done the trick: https://stackoverflow.com/a/37672385/8810083 – user8810083 Jul 03 '19 at 11:27
  • yes after Api26 we have ask permission like this..happy to help you – Prachi Singh Jul 03 '19 at 11:31
1

I use the Picasso library to make this easy. I save a copy of the image as a .png into the internal directory for the app so I don't have to worry about what happens to the original, and then can open it as a Texture in the game.

Picasso also includes methods for shrinking the image before it is loaded as a Bitmap, because you probably don't want a full resolution photo loaded as a Texture.

First add to android dependencies:

implementation 'com.squareup.picasso:picasso:2.5.2' 
// might need compile keyword instead if using old build tools

Then you can do this in your activity:

private static final int REQUEST_PICK_IMAGE = 1;
void getImage (){
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("image/*");
    startActivityForResult(intent, REQUEST_PICK_IMAGE );
}

public void onActivityResult(int requestCode, int resultCode, final Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_PICK_IMAGE && resultCode == Activity.RESULT_OK && data != null){

            // Check for file availability
            InputStream inputStream = null;
            try {
                inputStream = getContentResolver().openInputStream(data.getData());
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                Toast.makeText(this, "Image could not be accessed", Toast.LENGTH_SHORT).show();
                return;
            } finally {
                if (inputStream != null)
                    inputStream.close();
            }

            //TODO show progress bar and disable buttons

            new ImageResultTask().execute(data);
    }
}

private class ImageResultTask extends AsyncTask<Intent, Void, Boolean> {

    protected Boolean doInBackground (Intent... params) {
        FileOutputStream os = null;
        try {
            Bitmap bitmap = Picasso.with(MyActivity.this)
                .load(params[0].getData())
                .error(R.drawable.my_backup_drawable) // shouldn't happen, checked for FNFE above
                .resize(1024, 0) // the zero makes it scale to width and keep aspect ratio for height
                .get();
            getFilesDir(); // workaround for Android bug #10515463
            File destDir = getFilesDir();
            File destFile = new File(dir, "myImage.png");
            destFile.createNewFile();
            os = new FileOutputStream(destFile , false);
            bitmap.compress(Bitmap.CompressFormat.PNG, 0, os);
            os.flush();
            return true;
        } catch (IOException e){
            e.printStackTrace();
        } catch (SecurityException e){
            e.printStackTrace();
        } finally {
            if (os != null)
                os.close();
        }
        return false;
    }

    protected void onPostExecute(Boolean result) {
        if (result) {
            Gdx.app.postRunnable(...); //TODO call into game to tell it image is ready
            // The Texture can be loaded with Gdx.files.local("myImage.png")
        } else {
            //TODO show some error message
        }
        //TODO remove progress bar and unlock buttons, etc.
    }
}
Tenfour04
  • 83,111
  • 11
  • 94
  • 154
  • I haven't been able to get it to compile as is, but have something compiling and I think I may be creating myImage.png, but not sure and not sure how to declare it in the Texture declaration. Is your texture declaration simply 'Texture tex = new Texture("myImage.png");'? I don't think it's finding the file like that for me. And just to be clear, we are talking about libgdx textures right? – user8810083 Jul 03 '19 at 14:37
  • ok I can confirm that the myImage.png file IS definitely being created successfully. My main issue now is that it is not being found when I try to create the texture. In the android studio Device File Explorer I can see the file under data/data/[my package name]/files/myImage.png. I tried passing that entire path to the texture constructor but doesn't work. I also tried passing the destdir /data/user/0/[my package name]/files/myImage.png but that doesn't work either unfortunately. – user8810083 Jul 03 '19 at 14:48
  • Use `new Texture(Gdx.files.local("myImage.png"), true)`. The true is for mip-maps, which you probably want for anything besides text or pixel art. Android's `getFilesDir()` points to the same place as a `Gdx.files.local()`. It is inconsistent across versions and builds of Android, so don't try to work with the actual complete path. – Tenfour04 Jul 03 '19 at 14:56
  • thanks a lot! i'm finally able to see the gallery images in my game :). i didnt use picasso library yet, but might try tomorrow. i have it working with a hodgepodge of different peoples suggestions atm – user8810083 Jul 03 '19 at 15:11