简体   繁体   English

选择图库中的图像

[英]Choose Image in Gallery

I have this code, but when I click at the Button the Gallery will open and when I choose a image the Gallery will close again. 我有此代码,但是当我单击按钮时,图库将打开,而当我选择图像时,图库将再次关闭。 And nothing will happen. 什么也不会发生。

public class MainActivity extends AppCompatActivity { 公共类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();
    }
}

What is wrong? 怎么了?

I hope this help you : 希望对您有帮助:

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

and in onActivityResult(); 并在onActivityResult();中;

imageView = (ImageView)findViewById(R.id.imageView);
imageView.setImageBitmap(BitmapFactory.decodeFile(getRealPathFromUri(uri)));
@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);
            }
    }
}

and in your handlePhotoUpload method you can set the ImageView to the Bitmap 并在handlePhotoUpload方法中将ImageView设置为Bitmap

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM