如何从android 4.4中的图库中选择照片

时间:2014-03-02 08:36:41

标签: android image

我是android新手。我想从画廊中挑选图片。对于4.3及以下以下代码工作正常。但不是4.4。请帮忙。

buttonLoadImage.setOnClickListener(new View.OnClickListener(){

    @Override
    public void onClick(View arg0) {

        Intent i = new Intent(
                Intent.ACTION_PICK,
                android.provider.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 && 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();

    ImageView imageView = (ImageView) findViewById(R.id.imageView1);
    imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

}

1 个答案:

答案 0 :(得分:0)

看起来他们已完全改变了从Android 4.4中的图库中选择照片的方式,您必须使用此处所述的存储访问框架:

Storage Access Framework