从相机拍照并从图库中选择并在图像视图中显示

时间:2011-09-05 04:54:58

标签: android imageview

我想从画廊拍摄照片并将其设置在我的ImageView中,以及我想从相机拍摄照片并将其设置为我的相同图像视图。

我非常坚持这一点。

任何人都可以帮助我吗?

感谢。

3 个答案:

答案 0 :(得分:11)

您可以通过以下方式处理相机视图:

cameraImageView.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
            startActivityForResult(intent, 0);

        }
    });

在捕获图像后返回时,在活动中执行此操作。

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

    if(requestCode == 0)
    {
        if(data != null)
        {
            Bitmap photo = (Bitmap) data.getExtras().get("data");
            photo = Bitmap.createScaledBitmap(photo, 80, 80, false);
            imageView.setImageBitmap(photo);
        }
        else{
        }
    }
}

答案 1 :(得分:3)

获取图库的开源代码。 Lookout onclick用于图像选择。通过在onclick上设置意图uri来启动您的活动。

在你的应用程序中,获取意图数据并从uri获取真实路径然后解码,将其设置为图像视图元素。

private String getRealPathFromURI(Uri contentUri) {
    int columnIndex = 0;

    String[] proj = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(contentUri, proj, null, null, null);

    try {
        columnIndex = cursor.getColumnIndexOrThrow
                       (MediaStore.Images.Media.DATA);
    } catch (Exception e) {
        Toast.makeText(ImageEditor.this, "Exception in getRealPathFromURI",
                       Toast.LENGTH_SHORT).show();
        finish();  
        return null;
    }       
    cursor.moveToFirst();
    return cursor.getString(columnIndex);               
}

答案 2 :(得分:2)

您必须实现此代码才能从相机或图库中获取图片:

Take this variable : 

AlertDialog dialog;
private static final int IMAGE_PICK = 1;
private static final int IMAGE_CAPTURE = 2;
private Bitmap profile_imageBitmap;

按钮点击事件:

if (v == btn_uploadPhoto) {
    dialog.show();
    }

然后在你的活动的oncreate方法中:

final String[] items = new String[] { "Take from camera",
            "Select from gallery" };
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.select_dialog_item, items);
    AlertDialog.Builder builder = new AlertDialog.Builder(this);

    builder.setTitle("Select Image");
    builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) {

            if (item == 0) {
                path = "";
                Intent intent = new Intent(
                        "android.media.action.IMAGE_CAPTURE");
                File folder = new File(Environment
                        .getExternalStorageDirectory() + "/LoadImg");

                if (!folder.exists()) {
                    folder.mkdir();
                }
                final Calendar c = Calendar.getInstance();
                String new_Date = c.get(Calendar.DAY_OF_MONTH) + "-"
                        + ((c.get(Calendar.MONTH)) + 1) + "-"
                        + c.get(Calendar.YEAR) + " " + c.get(Calendar.HOUR)
                        + "-" + c.get(Calendar.MINUTE) + "-"
                        + c.get(Calendar.SECOND);

                path = String.format(
                        Environment.getExternalStorageDirectory()
                                + "/LoadImg/%s.png", "LoadImg(" + new_Date
                                + ")");
                File photo = new File(path);
                intent.putExtra(MediaStore.EXTRA_OUTPUT,
                        Uri.fromFile(photo));
                startActivityForResult(intent, 2);

            } else { // pick from file
                Intent intent = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                intent.setType("image/*");
                startActivityForResult(
                        Intent.createChooser(intent, "Choose a Photo"),
                        IMAGE_PICK);
            }
        }
    });

    dialog = builder.create();

现在离开了Oncreate:

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

    if (resultCode == Activity.RESULT_OK && requestCode == IMAGE_PICK
            || requestCode == IMAGE_CAPTURE) {
        switch (requestCode) {
        case IMAGE_PICK:
            this.imageFromGallery(resultCode, data);

            img_myProfile.setImageBitmap(null);

            img_myProfile.setImageBitmap(setphoto);

            break;

        case IMAGE_CAPTURE:

            this.imageFromGallery(resultCode, data);

            img_myProfile.setImageBitmap(null);

            img_myProfile.setImageBitmap(setphoto);

            break;
        default:
            break;
        }
    }
}

private void imageFromGallery(int resultCode, Intent 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]);

    profile_Path = cursor.getString(columnIndex);
    cursor.close();

    setphoto = BitmapFactory.decodeFile(profile_Path);

}

private void imageFromCamera(int resultCode, Intent data) {
    updateImageView((Bitmap) data.getExtras().get("data"));
}

private void updateImageView(Bitmap newImage) {
    setphoto = newImage.copy(Bitmap.Config.ARGB_8888, true);
}

public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}