Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何从gallery中获取图像并在imageview中显示?_Android_Android Imageview_Image Gallery - Fatal编程技术网

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

Android 如何从gallery中获取图像并在imageview中显示?,android,android-imageview,image-gallery,Android,Android Imageview,Image Gallery,如何在安卓系统中点击一个按钮就能从图库中获取图像? 我是android新手,请帮助。检查此代码非常容易。祝你好运 public class MainActivity extends Activity { Button btn1; ImageView img; private static int LOAD_IMAGE_RESULTS = 1; Bitmap mBitmap; @Override protected void onCreate(Bundle savedInstanceStat

如何在安卓系统中点击一个按钮就能从图库中获取图像?
我是android新手,请帮助。

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

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

你将在活动结果上获得图片路径

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

您可以使用它来获取完整的代码

使用以下代码,它可以完美地工作:

@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);

        }
    });
 }
在yout oncreate之外:

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

祝你好运:)

也许这个问题对你有帮助,有很多文章/教程可供参考。在这里发布问题之前,你至少在谷歌上搜索过吗?这里剽窃,有几个非常小的改动:
@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);
    }       
}