如何在Android中捕获图像并将其显示在图库中?

时间:2009-10-14 17:17:33

标签: android image-capture

我在画廊中出现之前拍摄的图像,但现在它们不是,我无法弄清楚原因。这是我的代码:

        ContentValues values = new ContentValues();
    values.put(android.provider.MediaStore.Images.Media.IS_PRIVATE, false);
    String name = "ugc_" + String.valueOf(System.currentTimeMillis());
    values.put(android.provider.MediaStore.Images.Media.TITLE, name);
    imageURI = getContentResolver().insert(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

我也尝试过:     values.put(android.provider.MediaStore.Images.Media.IS_PRIVATE,0); 以及留在那个价值,但现在似乎没有任何作用。

1 个答案:

答案 0 :(得分:7)

非常不漂亮,但这就是我这样做的方式。请注意due to a bug,保存的文件是完整分辨率的1/16(每个维度的1/4)。

String SD_CARD_TEMP_DIR = Environment.getExternalStorageDirectory() + File.separator + "tmpPhoto.jpg";
Intent takePictureFromCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePictureFromCameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(new
            File(SD_CARD_TEMP_DIR)));
startActivityForResult(takePictureFromCameraIntent, TAKE_PICTURE_WITH_CAMERA);

。 。

public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //  Picture taken from camera
        if (requestCode == TAKE_PICTURE_WITH_CAMERA) {
            if (resultCode == Activity.RESULT_OK) {

                // http://code.google.com/p/android/issues/detail?id=1480
                //Toast.makeText(AddPhotos.this, "" + data, Toast.LENGTH_SHORT).show();

                // on activity return
                File f = new File(SD_CARD_TEMP_DIR);
                try {
                    Uri capturedImage =
                        Uri.parse(android.provider.MediaStore.Images.Media.insertImage(getContentResolver(),
                                f.getAbsolutePath(), null, null));


                        Log.i("camera", "Selected image: " + capturedImage.toString());

                    f.delete();
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


            }
            else {
                Log.i("Camera", "Result code was " + resultCode);

            }
        }   
     }