cooktheprogrammer
cooktheprogrammer

Reputation: 57

How to load an image having its URI?

I'm having a really hard time trying to figure out how to load an image when I have its URI. The app works in the following manner:

I'm checking in the SQLite database whether a user exists and then I'm starting DescriptionActivity (code below). If the user had already been added to database I want to get the description from the database and load it into EditText (descFromDb[0] - no problem here) and get the URI from database in order to load the image into ImageView component (descFromDb[1] - lots of problems here), the logs from the Logcat are pasted below the class code. There's also some code for the case when user wants to upload a pic from the gallery and a button that updates the description and image URI in database.

I already added WRITE_EXTERNAL_STORAGE, READ_EXTERNAL_STORAGE and MANAGE_DOCUMENTS permissions to my manifest file however this still doesn't allow me to load the image based on its URI.

I read some threads that advised to use Intents and write code in a similar manner to how I set the image from gallery but tbh I don't really have a lot of experience in use of Intents other than starting a new Activity with layout added to it and those examples were quite confusing.

TL;DR I need to set the picture to ImageView while only having its URI - image.setImageURI(imgUri); doesn't work.

package com.example.kuba.myapplication;

import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import java.util.List;

import static com.example.kuba.myapplication.MainActivity.kidDatabase;

public class DescriptionActivity extends AppCompatActivity {
    public static final int PICK_IMAGE = 1;
    private static final String KIDS_DB = "kids_db";
    private int kidId;
    EditText descText;
    Button imgBtn;
    Button updateBtn;
    ImageView image;
    Uri imgUri = null;
    Button refreshBtn;

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == PICK_IMAGE) {
            imgUri = data.getData();
            image.setImageURI(imgUri);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_desc);
        kidId = getIntent().getIntExtra("KID_ID", -1);
        image = (ImageView) findViewById(R.id.imageView);
        imgBtn = (Button) findViewById(R.id.picBtn);
        refreshBtn = (Button) findViewById(R.id.refreshBtn);
        descText = (EditText) findViewById(R.id.descEditText);


        final String[] descFromDb = new String[2];
        new Thread(new Runnable() {
            @Override
            public void run() {
                descFromDb[0] = kidDatabase.daoAccess().getDesc(kidId);
                descFromDb[1] = kidDatabase.daoAccess().getImgPath(kidId);
                if (descFromDb[0] != null) {
                    descText.setText(descFromDb[0]);
                }
                if (descFromDb[1] != null) {
                    imgUri = Uri.parse(descFromDb[1]);
                    image.setImageURI(null);
                    image.setImageURI(imgUri);
                }
            }
        }).start();

        refreshBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                image.setImageURI(imgUri);
            }
        });
        imgBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);
            }

        });

        updateBtn = (Button) findViewById(R.id.saveBtn);
        updateBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Kid kid = kidDatabase.daoAccess().fetchKidById(kidId);
                        if (kid == null) {
                            return;
                        } else {
                            String description = descText.getText().toString();
                            kidDatabase.daoAccess().updateDescAndPath(kidId, description, imgUri.toString());
                        }
                    }
                }).start();
                Toast.makeText(getApplicationContext(), "Desc updated", Toast.LENGTH_LONG).show();
            }
        });
    }
}


04-28 15:02:23.264 16941-17042/com.example.kuba.myapplication I/WAR:: imguri: content://com.android.providers.media.documents/document/image%3A47
04-28 15:02:23.267 16941-17042/com.example.kuba.myapplication W/ImageView: Unable to open content: content://com.android.providers.media.documents/document/image%3A47
    java.lang.SecurityException: Permission Denial: opening provider com.android.providers.media.MediaDocumentsProvider from ProcessRecord{fbe1f29 16941:com.example.kuba.myapplication/u0a84} (pid=16941, uid=10084) requires android.permission.MANAGE_DOCUMENTS or android.permission.MANAGE_DOCUMENTS
        at android.os.Parcel.readException(Parcel.java:1684)
        at android.os.Parcel.readException(Parcel.java:1637)
        at android.app.ActivityManagerProxy.getContentProvider(ActivityManagerNative.java:4199)
        at android.app.ActivityThread.acquireProvider(ActivityThread.java:5476)
        at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2239)
        at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1517)
        at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1131)
        at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:984)
        at android.content.ContentResolver.openInputStream(ContentResolver.java:704)
        at android.widget.ImageView.getDrawableFromUri(ImageView.java:900)
        at android.widget.ImageView.resolveUri(ImageView.java:871)
        at android.widget.ImageView.setImageURI(ImageView.java:490)
        at android.support.v7.widget.AppCompatImageView.setImageURI(AppCompatImageView.java:115)
        at com.example.kuba.myapplication.DescriptionActivity$1.run(DescriptionActivity.java:75)
        at java.lang.Thread.run(Thread.java:761)
    resolveUri failed on bad bitmap uri: content://com.android.providers.media.documents/document/image%3A47
04-28 15:02:23.378 16941-16946/com.example.kuba.myapplication I/art: Do partial code cache collection, code=60KB, data=59KB
    After code cache collection, code=60KB, data=59KB
    Increasing code cache capacity to 256KB

Upvotes: 0

Views: 1496

Answers (1)

Suleyman
Suleyman

Reputation: 2953

Just use an image loading library, for example Glide, add the dependency:

implementation "com.github.bumptech.glide:glide:4.7.1"

It must be compiled against compile SDK version v27 or higher. And it uses support library v27, if you want to use a lower version of the support library then refer here

And finally the usage:

Glide.with(this)
.load(url)
.into(imageView);

Upvotes: 2

Related Questions