How can I capture an image in Android and have it show up in the gallery?

萝らか妹 提交于 2019-12-06 08:33:47

问题


I was capturing images before that were showing up in the gallery, but now they are not and I can't figure out why. Here is my code:

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

I've also tried: values.put(android.provider.MediaStore.Images.Media.IS_PRIVATE, 0); as well as leaving at that value, but nothing seems to work now.


回答1:


Very much not pretty, but this is how I'm doing it. Note that due to a bug, the file saved is 1/16 the full resolution (1/4 in each dimension).

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

            }
        }   
     }


来源:https://stackoverflow.com/questions/1567732/how-can-i-capture-an-image-in-android-and-have-it-show-up-in-the-gallery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!