【问题标题】:Image taken from gallery doesn't appear从图库拍摄的图像不显示
【发布时间】:2017-06-15 18:10:36
【问题描述】:

我使用 Picasso 从图库中获取图像并将其设置为 ImageView,但它没有这样做。找不到问题。是什么原因?有趣的是没有错误。我通过自己的设备测试了该程序。

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);
    }
}

【问题讨论】:

  • 使用Picasso.Listener to see if there was an error in Picasso
  • 我注意到调用了 onImageLoadFailed 方法。但不明白为什么?
  • 如果您使用了我链接到的答案中的代码,LogCat 中将有一个 Java 堆栈跟踪来解释问题所在。
  • 你使用的是哪个版本的安卓?
  • @AjeetChoudhary 最新版本 2.2.3

标签: android imageview picasso


【解决方案1】:

你打电话给super.onActivityResult(requestCode, resultCode, data);这是错误的。

这样做

@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
    }
}

【讨论】:

  • 请详细解释这将如何影响毕加索加载图像的能力。
  • 你试试看,请告诉我
  • @quicklearner 无论如何,它对我不起作用。我注意到该程序的 if 语句有效。这意味着必须设置图像。但为什么它不起作用?
  • 是的,当您在活动结果中获取数据时,您必须对她进行图像处理
【解决方案2】:

问题已解决。

我忘记添加用于读取外部存储的权限。

【讨论】:

    猜你喜欢
    • 2018-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-18
    相关资源
    最近更新 更多