Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android takePersistableUriPermission doesn't compile

I'm using KITKAT content provider in my application. After getting the uri of a selected picture I store it in a database to use it again later. The problem is that after restarting the phone I got a permission exception while using the stored uri.

After searching I tried to use takePersistableUriPermission() But for some reason it doesn't compile (I use androidstudio).

The error is : Must be one or more of IntentFLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION . It occurs on the second argument of the method takePersistableUriPermission (the flag)

Here is the code:

Intent intent;
            if (Build.VERSION.SDK_INT < 19){
                intent = new Intent();
                intent.setAction(Intent.ACTION_GET_CONTENT);
                intent.setType("image/*");
                startActivityForResult(intent, ANDROID_LESS_19);
            } else {
                intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
                intent.addCategory(Intent.CATEGORY_OPENABLE);
                intent.setType("image/*");
                startActivityForResult(intent, ANDROID_MORE_19);
            }`

 case ANDROID_MORE_19:
            if(resultCode == RESULT_OK){
                Uri selectedImage = data.getData();
                mImage = selectedImage.toString();
                String[] filePathColumn = {MediaStore.Images.Media.DATA};

                // this part means to keep permanent permissions, so no exception after rebooting the device
                final int takeFlags = data.getFlags()
                        & (Intent.FLAG_GRANT_READ_URI_PERMISSION
                        | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

                getContentResolver().takePersistableUriPermission(selectedImage, takeFlags);

                grantUriPermission("com.main.bonappetit", selectedImage, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);

                Cursor cursor = getContentResolver().query(
                        selectedImage, filePathColumn, null, null, null);
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String filePath = cursor.getString(columnIndex);
                cursor.close();

Someone knows what's wrong here?

like image 284
totomac Avatar asked Jan 31 '15 16:01

totomac


1 Answers

On Android Studio, I had to decompose the line to be able to compile :

int takeFlags = data.getFlags();
takeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
like image 165
Jerome Avatar answered Nov 04 '22 08:11

Jerome