[英]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.