选择图库中的图像

时间:2016-11-10 20:29:17

标签: java android

我有这个代码,但是当我点击按钮时,画廊将会打开,当我选择一个图像时,画廊将再次关闭。什么都不会发生。

公共类MainActivity扩展了AppCompatActivity {

private Button loadimagebutton;
private ImageView imageView;

private static int RESULT_LOAD_IMAGE = 1;

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

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

    loadimagebutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(i, RESULT_LOAD_IMAGE);
        }
    });
}

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)findViewById(R.id.imageView);
        imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
    }
    else{
        Toast.makeText(getApplicationContext(), "ERROR", Toast.LENGTH_LONG).show();
    }
}

有什么问题?

2 个答案:

答案 0 :(得分:1)

我希望这可以帮到你:

private String getRealPathFromUri(Uri contentURI) {
    String result = "";
    Cursor cursor = getContentResolver().query(contentURI, null, null, null, null);
    if (cursor == null) {
        result = contentURI.getPath();
    } else {
        cursor.moveToFirst();
        int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
        result = cursor.getString(idx);
        cursor.close();
    }
    return result;
}

和onActivityResult();

imageView = (ImageView)findViewById(R.id.imageView);
imageView.setImageBitmap(BitmapFactory.decodeFile(getRealPathFromUri(uri)));

答案 1 :(得分:1)

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

    switch (requestCode) {
        case RESULT_LOAD_IMG:
            if (resultCode == RESULT_OK) {
                Uri selectedImage = imageReturnedIntent.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 filePath = cursor.getString(columnIndex);
                cursor.close();


                Bitmap bMap = BitmapFactory.decodeFile(filePath);
                handlePhotoUpload(bMap);
            }
    }
}

并且在handlePhotoUpload方法中,您可以将ImageView设置为位图