ImageView未显示从库中选择的图像

时间:2015-01-26 18:03:07

标签: java

所以我有一个加载图库的按钮,并使用此代码选择图像......

public void getGalleryImage(View v){
    Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
    startActivityForResult(intent, 1);
}

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

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

        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

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

        galleryImage.setImageBitmap(BitmapFactory.decodeFile(picturePath));

    }


}

但是当它返回到原始活动时,ImageView仍然没有显示任何内容。它没有给我任何错误,或类似的东西。这里是ImageView的XML ...

<ImageView
    android:id="@+id/galleryImage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

我怎样才能展示它?

3 个答案:

答案 0 :(得分:2)

我最终想出了什么。我刚刚做了......

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

    }

这适用于我想要完成的任务。

答案 1 :(得分:1)

试试这个:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
        Uri selectedImage = data.getData();
        galleryImage.setImageBitmap(MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri));
    }
}

答案 2 :(得分:0)

  public class MainActivity extends AppCompatActivity{

    ImageView image;
    Button pick;

    int TAG_IMAGE = 100;


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

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

        pick = (Button) findViewById(R.id.button);

        pick.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                intent.setType("*/*");


                startActivityForResult(Intent.createChooser(intent,"Select Image "),TAG_IMAGE);

            }
        });


    }

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

        if(requestCode == TAG_IMAGE && resultCode == RESULT_OK )
        {
            Uri selectedImage = data.getData();


            try
            {

                String[] filePath = {MediaStore.Images.Media.DATA};

                Cursor cursor = getContentResolver().query(selectedImage,filePath,null,null,null);
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePath[0]);

                String images = cursor.getString(columnIndex);



                image.setImageURI(selectedImage);


            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }


    }



}