如何从图库中获取图像并在imageview中显示?

时间:2014-04-08 12:18:06

标签: android android-imageview image-gallery

如何通过点击Android中的按钮从图库中获取图像? 我是android的新手,请帮忙。

3 个答案:

答案 0 :(得分:0)

检查此代码非常容易。祝你好运

public class MainActivity extends Activity {

Button btn1;
ImageView img;
private static int LOAD_IMAGE_RESULTS = 1;
Bitmap  mBitmap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btn1 = (Button) findViewById(R.id.button1);
    edit = (EditText) findViewById(R.id.editText1);
    img = (ImageView) findViewById(R.id.ImageView1);

    img.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

            // Start new activity with the LOAD_IMAGE_RESULTS to handle back the results when image is picked from the Image Gallery.
            startActivityForResult(i, LOAD_IMAGE_RESULTS);

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

    // Here we need to check if the activity that was triggers was the Image Gallery.
    // If it is the requestCode will match the LOAD_IMAGE_RESULTS value.
    // If the resultCode is RESULT_OK and there is some data we know that an image was picked.
    if (requestCode == LOAD_IMAGE_RESULTS && resultCode == RESULT_OK && data != null) {
        // Let's read picked image data - its URI
        Uri pickedImage = data.getData();
        // Let's read picked image path using content resolver
        String[] filePath = { MediaStore.Images.Media.DATA };
        Cursor cursor = getContentResolver().query(pickedImage, filePath, null, null, null);
        cursor.moveToFirst();
        imagePath = cursor.getString(cursor.getColumnIndex(filePath[0]));

        // Now we need to set the GUI ImageView data with data read from the picked file.
        mBitmap = BitmapFactory.decodeFile(imagePath);
        img.setImageBitmap(BitmapFactory.decodeFile(imagePath));

        // At the end remember to close the cursor or you will end with the RuntimeException!
        cursor.close();
    }
}

}

答案 1 :(得分:0)

Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);

你将在onActivityResult上获得图片路径

@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.imgView);
            imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
        }
    }

并使用此功能获取完整代码HERE

答案 2 :(得分:0)

使用以下代码完美地运作:

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ImageView imageView = (ImageView) findViewById(R.id.imgView);
    Button yourbutton = (Button) ( findViewById(R.id.button);

    yourbutton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent, TAKE_PICTURE);

        }
    });
 }
在你外面创造:

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


    switch(requestCode){

        case TAKE_PICTURE:             
            if(resultCode==Activity.RESULT_OK) {

                // get bundle
                Bundle extras = data.getExtras();

                // get 
                takenPictureData = (Bitmap) extras.get("data");
            //  imageView.setImageBitmap(bitMap);
            }               
            break;  
    }

    //And show the result in the image view when take picture from camera.
    if(takenPictureData!=null){
        imageView.setImageBitmap(takenPictureData);
    }       
}

祝你好运:)