0

Im trying to do the next:

  1. Find image from device and add it to programm (done!)
  2. Store image URI to string and save it to .txt file (done!)
  3. Extract URI from .txt file (done!)
  4. Open a dialog window to show the image on click - and there is a problem - i've tryied everything i found on google and stack overflow - both methods does not work - drawable returns no image (drawable = null), and setImageURI crashes the application. Debugger shows something like that:

content://com.android.providers.media.documents/document/image%3A31

code:


public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.pop_up_add_item, null);
        final Button btn=view.findViewById(R.id.test);
        imgg=view.findViewById(R.id.img);
        String str_tooltip=getArguments().getString("Entity");
        String main_str=getArguments().getString("Original");
        String str_adress=getArguments().getString("Uri");
        Uri uri = Uri.parse(str_adress);

        //imgg.setI(uri);


        if (str_adress!=null & str_adress.length()>0) {
            final Uri uri1 = Uri.parse(str_adress);
            //final String path = uri.getPath();
            //File drawableFile = new     File(getApplicationContext().getFilesDir().getAbsolutePath()+"/into11.png");
            //final Drawable drawable = Drawable.createFromPath(path);

            imgg.setImageURI(uri);

        } 

Is there any solution on this? Probably common problem?

UPDATE

After tried all the available options eventually (thanks to sir @Dilshad) i've found that the trouble is caused by java.lang.SecurityException: Permission Denial: opening provider com.android.providers.media.MediaDocumentsProvider from ProcessRecord{1a24ed9 11931:com.example.myapplication/u0a121} (pid=11931, uid=10121) requires that you obtain access using ACTION_OPEN_DOCUMENT or related APIs --> Permission issue.

So, trying to solve it i added:

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" tools:ignore="ScopedStorage"/>

in AndroidManifest

and int result = ContextCompat.checkSelfPermission(takeThis, android.Manifest.permission.READ_EXTERNAL_STORAGE); in the place in the code where the access begins.

But till the moment problem is not solved.

PewPew01
  • 23
  • 1
  • 6
  • But please, answering this be patient, im kinda new to Java and Android and some short and simple explanation may just confuse me:) – PewPew01 Feb 20 '23 at 01:15
  • what is the error message you see in log – Dilshad Feb 20 '23 at 04:25
  • Null exception i guess – PewPew01 Feb 20 '23 at 13:22
  • guess?, sir please copy paste the error you see in Logcat – Dilshad Feb 20 '23 at 14:06
  • Was from phone, sorry. There is actual log item: `java.lang.SecurityException: Permission Denial: opening provider com.android.providers.media.MediaDocumentsProvider from ProcessRecord{ae0c997 11188:com.example.myapplication/u0a121} (pid=11188, uid=10121) requires that you obtain access using ACTION_OPEN_DOCUMENT or related APIs` Seems to be permission problem. Trying to work around, but still the same. Interesting thing - when i create my object first time and access virtual machine item (screenshot) - it works fine and opens on all entities, but on reload it crashes while try to open – PewPew01 Feb 20 '23 at 14:36
  • check out https://stackoverflow.com/questions/46916992/having-trouble-implementing-action-open-document-to-my-project – Dilshad Feb 20 '23 at 14:50
  • @Dilshad I've checked this question and the solution were so close, but when i paste code it not works (some of the entities are cannot be loaded as classes, etc.). Im not understanding what intent is, what context is, etc. I'm few days in android developement and trying to do some very common things, that becomes huge mess. How do i ask user to give permissions? Is there any simple solution? Like for example when you first open an app - it asks for 5-6 permissions needed. Maybe you know the solution? Please :) – PewPew01 Feb 20 '23 at 15:14
  • if you are a beginner i would say you should move to Kotlin instead of Java and write UI in Jetpack Compose – Dilshad Feb 22 '23 at 02:42
  • @Dilshad Kotlin have like 5% of the Java support. Java is widely represented in internet, every of my issues are already appear somewhere and solved. Kotlin has nothing of that. But thanks for advice. – PewPew01 Feb 22 '23 at 13:31
  • sorry for discussing off-topic from your question. Java is just older but Kotlin is way functional for android. I never felt lack of resources in internet for Kotlin in my journey. there is nothing wrong using java for android but your gonna miss lot of things especially writing UI in Compose and KMM. – Dilshad Feb 23 '23 at 05:18

0 Answers0