其他分享
首页 > 其他分享> > 如何在Android中捕获图像并将其显示在图库中?

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

作者:互联网

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

        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);
以及留在那个价值,但现在似乎没有任何作用.

解决方法:

非常不漂亮,但这就是我这样做的方式.请注意,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);

            }
        }   
     }

标签:android,image-capture
来源: https://codeday.me/bug/20190823/1696749.html