简体   繁体   English

无法从图库中将图像加载到imageView中

[英]Cannot Load image in imageView from gallery

I want to load an image in an image view from the gallery on the phone and I have written the code java: 我想从手机的图库中以图像视图的形式加载图像,并且编写了代码java:

public class MainActivity extends AppCompatActivity {

private static int RESULT_LOAD_IMAGE = 1;
Button btnTry , btnAdd;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btnAdd = findViewById(R.id.btnAdd);
    btnTry = findViewById(R.id.btnTry);

    btnAdd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            //The integer argument is a "request code" that identifies your request. When you receive the result Intent,
            // the callback provides the same request code so that your app can properly identify the result and determine how to handle it.

            Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(i ,RESULT_LOAD_IMAGE);
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode == RESULT_LOAD_IMAGE && requestCode == RESULT_OK && null != data){
        Uri selectImage = data.getData();
        String[] filePathcolumn = {MediaStore.Images.Media.DATA};

        Cursor cursor = getContentResolver().query(selectImage , filePathcolumn , null , null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathcolumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();

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

ivMemeImage.setImageBitmap(BitmapFactory.decodeFile(picturePath));
    }
}
}

when I run the app and hit the add button image does not load in the image view. 当我运行应用程序并单击添加按钮时,图像未加载到图像视图中。 and I have given both the read and write permission in the manifest. 而且我已经在清单中同时授予了读取和写入权限。

You can display your image using your Uri. 您可以使用Uri显示图像。 you only need to do is set your imageView.setImageUri(selectedImage) 您只需要设置您的imageView.setImageUri(selectedImage)

if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && data != null) {
  Uri selectedImage = data.getData();
  your_imageView.setImageURI(selectedImage);
}

is this code you have done in android studio? 这是您在android studio中完成的代码?

    if(requestCode == RESULT_LOAD_IMAGE && requestCode == RESULT_OK && null != data)

it should be 它应该是

    if(requestCode == RESULT_LOAD_IMAGE && requestCode == RESULT_OK && data!=null)

Your code should give you compile time error 您的代码应该给您编译时错误

check out Picasso library .. http://square.github.io/picasso/ 查看Picasso库.. http://square.github.io/picasso/

load image from a URI into imageview with one line of code: 使用一行代码将URI中的图像加载到imageview中:

Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);

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

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