简体   繁体   English

Android将图像从路径加载到imageView

[英]Android Load image from path to imageView

I am trying to load image from file path in local storage to an imageView. 我正在尝试将图像从本地存储中的文件路径加载到imageView。

private final String PATH = "file:///storage/emulated/0/Downloads/image.png"

As suggested elsewhere in the forum I've tried to load it both using Glide: 正如论坛中其他地方所建议的那样,我尝试使用Glide加载它们:

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

and Bitmap/BitmapFactory: 和Bitmap / BitmapFactory:

ImageView imageView = (ImageView) findViewById(R.id.imageView);

    File f = new File(PATH);

    if(f.exists()){

        Bitmap myBitmap = BitmapFactory.decodeFile(f.getAbsolutePath());

        imageView.setImageBitmap(myBitmap);

but I always get the placeholder image. 但我总是得到占位符图像。

In the manifest I added the permission WRITE.EXTERNAL.STORAGE and READ.EXTERNAL.STORAGE. 在清单中,我添加了权限WRITE.EXTERNAL.STORAGE和READ.EXTERNAL.STORAGE。

Trying to debug it seems to load the correct path in f variable. 尝试调试似乎在f变量中加载了正确的路径。

Thanks for help 感谢帮助

Edit: I have tried all the suggested solution, but I continue gettin blank screen... 编辑:我已经尝试了所有建议的解决方案,但我继续得到空白屏幕...

I have also made a brand new app with one imageView only to better test: 我还制作了一个带有一个imageView的全新应用,目的只是为了更好地进行测试:

package com.example.android.pic;


import android.Manifest;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;

import com.bumptech.glide.Glide;

import java.io.File;

public class MainActivity extends AppCompatActivity {


Uri PATH = Uri.fromFile(new File((Environment.getExternalStorageDirectory().getPath() + "/Pictures/agata.png")));




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    int permissionCheck = ContextCompat.checkSelfPermission(this,
            Manifest.permission.WRITE_EXTERNAL_STORAGE);


    if (permissionCheck != PackageManager.PERMISSION_GRANTED) {

        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE)) {

        } else {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);

        }


    }
    ImageView imageView = (ImageView) findViewById(R.id.pic);

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

}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

    switch (requestCode) {
        case 1: {
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                ImageView imageView = (ImageView) findViewById(R.id.pic);

                Glide.with(this)
                        .load(PATH)
                        .into(imageView);
            } else {
                finish();

            }

        }
        default:
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}
}

please help! 请帮忙! Thanks 谢谢

Your file path deceleration is wrong. 您的文件路径减速错误。 Try changing PATH to 尝试将PATH更改为

private final StringPATH="/storage/emulated/0/Downloads/image.png"

You Should use this code 您应该使用此代码

final InputStream imageStream = getContentResolver().openInputStream(imageUri);

final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);

image.setImageBitmap(selectedImage);

in this code you want imageUri that can use StringPATH: 在此代码中,您需要可以使用StringPATH的imageUri:

private final StringPATH="/storage/emulated/0/Downloads/image.png"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM