如何加载具有URI的图像?

时间:2018-04-28 16:08:17

标签: android image sqlite uri loading

当我拥有其URI时,我正在努力弄清楚如何加载图像。 该应用程序以下列方式工作:

我正在检查SQLite数据库中是否存在用户然后我正在启动DescriptionActivity(下面的代码)。如果用户已经添加到数据库中,我想从数据库中获取描述并将其加载到EditText(descFromDb [0] - 这里没问题)并从数据库获取URI以便将图像加载到ImageView组件中(descFromDb) [1] - 这里有很多问题),来自Logcat的日志粘贴在类代码下面。当用户想要从图库上传图片时,还有一些代码用于更新数据库中描述和图像URI的按钮。

我已经为我的清单文件添加了WRITE_EXTERNAL_STORAGE,READ_EXTERNAL_STORAGE和MANAGE_DOCUMENTS权限,但是这仍然不允许我根据其URI加载图片。

我读了一些线程,建议使用Intents并以类似的方式编写代码,以便我从图库设置图像,但是我没有很多使用Intents的经验,除了开始一个新的Activity添加了布局,这些例子非常令人困惑。

TL; DR我需要将图片设置为ImageView,同时只有URI - image.setImageURI(imgUri);不起作用。

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

1 个答案:

答案 0 :(得分:0)

只需使用图片加载库,例如Glide,添加依赖项:

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

必须针对编译SDK版本v27或更高版本进行编译。它使用支持库v27,如果您想使用较低版本的支持库,请参考here

最后用法:

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