我有这个应用程序,您可以在其中拍照并保存在图库中,但是拍照后它们不会出现,我必须重新启动我的设备

I have this application where you take pictures and are saved in gallery , but after taking them do not appear, I have to restart my device

//这是我的代码

    public class MainActivity extends AppCompatActivity {

        int TAKE_PHOTO_CODE = 0;
        public static int count = 0;
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
           final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) + "/Camera/";
            File newdir = new File(dir);
           newdir.mkdirs();

            Button capture = (Button) findViewById(R.id.btnCapture);
            capture.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {

                    count++;
                    String file = dir + count + ".jpg";
                    File newfile = new File(file);
                    try {
                        newfile.createNewFile();
                    } catch (IOException e) {
                    }
                    Uri outputFileUri = Uri.fromFile(newfile);

                    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);

                    startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);


                }
            });

//Help with the refresh please
// I have this application where you take pictures and are saved in gallery , but after taking them do not appear, I have to restart my device

    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) {
            Log.d("CameraDemo", "Pic saved");
        }
    }
}

试试下面的代码,它对我有用,它在我们将它放入图库后立即显示图像,无需重新启动设备:

    private static final int REQUEST_CODE_CAMERA_PICTURE = 1001;
    private static final String SCAN_DOCUMENTS_DIR = "Pic_Saved_Directory";
    private String scanedImageAbsolutePath;

    public void openScanActivity() {
        Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        createDir();
        File scanedImageFile = new File(Environment.getExternalStorageDirectory(), SCAN_DOCUMENTS_DIR +"/image_"+ System.currentTimeMillis()+".jpg");
        scanedImageAbsolutePath = scanedImageFile.getAbsolutePath();
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(scanedImageFile));
        getActivity().startActivityFromFragment(this, intent, REQUEST_CODE_CAMERA_PICTURE);
    }

    private void createDir(){
        File directory = new File(Environment.getExternalStorageDirectory(), SCAN_DOCUMENTS_DIR);
        if (!directory.exists()) {
            directory.mkdirs();
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == REQUEST_CODE_CAMERA_PICTURE && resultCode == Activity.RESULT_OK) {
            //image will be available on : scanedImageAbsolutePath
            Toast.makeText(getActivity(), "Pic Saved", Toast.LENGTH_SHORT).show();

        } else {
            Toast.makeText(getActivity(), "Pic Not Saved", Toast.LENGTH_SHORT).show();
        }
    }

您还需要在 AndroidManifest 文件中提及以下权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />

我正在使用下一个activity,它用于显示拍摄的照片,并将拍摄的照片保存为位图。 您可以使用保存按钮将拍摄的图像保存在您的文件夹中。

String fileSavedPath;
        Log.d("tag", "Utility.storage_path : " + Utility.storage_path);
        File var4 = new File(Environment.getExternalStorageDirectory()
                .toString() + File.separator + Utility.storage_path); //your path
        var4.mkdirs();
        String var2 = (new SimpleDateFormat("yyyyMMdd_HH_mm_ss"))
                .format(new Date());
        var4 = new File(var4, "Frame_" + var2 + ".png");
        fileSavedPath = var4.getAbsolutePath();
        if (var4.exists()) {
            var4.delete();
        }
        try {
            FileOutputStream var5 = new FileOutputStream(var4);
            Utility.tempBitmap.compress(CompressFormat.JPEG, 80, var5); //convert your image in bitmap 
            var5.flush();
            var5.close();
            MediaScannerConnection.scanFile(ImageEditingActivity.this,
                    new String[] { fileSavedPath },
                    new String[] { "image/png" },
                    (OnScanCompletedListener) null);
            /*
             * Toast.makeText(ImageEditingActivity.this,
             * "Collage is saved at " + fileSavedPath, 0).show();
             */
        } catch (Exception var3) {
            var3.printStackTrace();
            ImageEditingActivity.this.runOnUiThread(new Runnable() {
                public void run() {
                    /*
                     * Toast.makeText(ImageEditingActivity.this,
                     * "Some thing wrong happen", 0).show();
                     */
                }
            });
        }