简体   繁体   English

从图库中拍摄的图像未出现

[英]Image taken from gallery doesn't appear

I use Picasso to get an image from the gallery and set it to an ImageView, but it does not do it. 我使用毕加索从图库中获取图像并将其设置为ImageView,但它没有这样做。 Couldn't find a problem. 找不到问题。 What is the reason? 是什么原因? And the interesting thing is that there was no error. 有趣的是,没有错误。 I tested the program through my own device. 我通过自己的设备测试了该程序。

public class MainActivity extends AppCompatActivity {
    String imageUri ;
    ImageView img ;
    private static final int GALLERY_REQUEST = 9391;
    Button b ;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == GALLERY_REQUEST && resultCode == RESULT_OK && data != null) {
            imageUri = data.getData().toString() ;
            loadImage() ;
        }
        else
        {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

    private void loadImage() {
        Picasso.with(this).load(imageUri).fit().centerInside().into(img);
    }

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


        img = (ImageView)findViewById(R.id.image);
        b = (Button)findViewById(R.id.button) ; // it is button used to open //a gallery
    }

    //thins function called when button pressed
    public void openGallery(View view) {
        Intent i = new Intent(ACTION_PICK,EXTERNAL_CONTENT_URI) ;

        startActivityForResult(i,GALLERY_REQUEST);
    }
}

You called super.onActivityResult(requestCode, resultCode, data); 您调用了super.onActivityResult(requestCode, resultCode, data); which is wrong . 这是错误的。

Do this 做这个

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == GALLERY_REQUEST && resultCode == RESULT_OK && data != null) {
        Uri selectedImageURI = data.getData();                 

 Picasso.with(this).load(selectedImageURI).fit().centerInside().into(img);
    }
    else
    {
        // handle this case
    }
}

Problem Solved. 问题解决了。

I forgot to add permission used to read external storage. 我忘记添加用于读取外部存储的权限。

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

相关问题 Android - 外部存储中保存的图像不会出现在图库中 - Android - Saved image in external storage doesn't appear in gallery 保存到 sdcard 的图像不会出现在 Android 的图库应用中 - Image, saved to sdcard, doesn't appear in Android's Gallery app 尝试构建一个程序,使用户能够从图库中获取 select 图像,但是当我运行并选择图像时,图像不会出现在应用程序中 - Trying to build a program that enables users to select image from gallery but when I run and choose the image the image doesn't appear in the app 从相机拍摄后将图像保存到图库 - save image to the gallery after taken from the camera 已保存的视频未出现在图库中 - saved video doesn't appear in gallery Flutter 图像选择器无法从相册/图库中选择图像 - Flutter Image Picker doesn't pick image from Album/Gallery 如何在图库中的imageView上设置图像以及在Android中由相机拍摄的图像? - How to set an image on imageView from the gallery and image taken by camera in Android? 从图库中获取的图像无法在图像视图中设置 - image taken from gallery is unable to set in image view 从图库中选择的图像未在Imageview中设置 - Selected image from gallery doesn't set in Imageview 从图库上传的图像未在ImageView中显示 - Image uploaded from gallery doesn't show in ImageView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM