HY,
我要清空内部文件夹“ / storage / emulated / 0 / DCIM / Camera”
以下代码对我有用

public void emptyDir()
    {
        File dir = new File("/storage/emulated/0/DCIM/Camera");
        if (dir.isDirectory())
        {
            String[] children = dir.list();
            for (int i = 0; i < children.length; i++)
            {
                new File(dir, children[i]).delete();
                Log.i(logTag,"Path " +dir +" cleared");
            }
        }
    }


我可以不使用硬编码访问/读取此路径吗?

捕获我的图片的代码:
我认为大多数代码都非常清楚我的意思,因为即使我只是在测试新的东西,我也会一直尝试评论我所做的任何事情。我希望我能展示我用这个创造的东西。

public void takePhoto()
{
    //Set the size of the Picture
    Parameters params = mCamera.getParameters();
    params.setPictureSize(640, 480);
    mCamera.setParameters(params);

    //Befehl um die möglichen Auflösungen auszuwählen/aufzulisten
    //List<Camera.Size> sizes = params.getSupportedPictureSizes();

    //Take the Picture
    mCamera.takePicture(null, null, mPicture);

    //SLEEP "sleeper value" SECONDS HERE ...
    Handler handler = new Handler();
    handler.postDelayed(new Runnable()
    {
        public void run()
        {
            releaseCamera();
            FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
            preview.removeAllViews();
            activateCamera();
        }
    }, sleeper);
}

/**
 * Creates and Instance of Camera and adds CameraView to
 * camera_preview FrameLayout
 */
public void activateCamera()
{
    // Create an instance of Camera
    mCamera = getCameraInstance();

    // Create our Preview view and set it as the content of our activity.
    mPreview = new CameraPreview(this, mCamera);
    FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
    preview.addView(mPreview);
}



final PictureCallback mPicture = new PictureCallback()
{

    public void onPictureTaken(byte[] data, Camera camera)
    {

        File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);

        if (pictureFile == null)
        {
            return;
        }

        try
        {
            FileOutputStream fos = new FileOutputStream(pictureFile);
            fos.write(data);

            fos.close();
            path = pictureFile.getAbsolutePath();
            System.out.println("Picture stored at: "+path);
            MediaStore.Images.Media.insertImage(getContentResolver(), pictureFile.getAbsolutePath(), pictureFile.getName(), pictureFile.getName());
        }
        catch (FileNotFoundException e)
        {

        }
        catch (IOException e)
        {

        }
    }
};


/** A safe way to get an instance of the Camera object. */
public static Camera getCameraInstance(){
    Camera c = null;
    try
    {
        c = Camera.open(); // attempt to get a Camera instance
    }
    catch (Exception e)
    {
        System.out.println("Camera in use");
        // Camera is not available (in use or does not exist)
    }
    return c; // returns null if camera is unavailable
}

@Override
protected void onPause()
{
    super.onPause();
    releaseCamera();              // release the camera immediately on pause event
}

private void releaseCamera()
{
    if (mCamera != null)
    {
        mCamera.release();        // release the camera for other applications
        mCamera = null;
    }
}

/** Create a File for saving an image or video */
private  File getOutputMediaFile(int type)
{
    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.

    //File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MFI Webcam");

    File rootsd = Environment.getExternalStorageDirectory();
    File mediaStorageDir = new File(rootsd.getAbsolutePath() + "/MFI Webcam");

    // This location works best if you want the created images to be shared
    // between applications and persist after your app has been uninstalled.

    // Create the storage directory if it does not exist
    if (! mediaStorageDir.exists())
    {
        if (! mediaStorageDir.mkdirs())
        {
            return null;
        }
    }

    // Create a media file name
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE)
    {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator + "webcam"+ ".jpg");
    } else
    {
        return null;
    }

    return mediaFile;
}

最佳答案

使用getExternalStoragePublicDirectory(DIRECTORY_DCIM),您可能会得到/storage/emulated/0/DCIM。但这绝不能保证图片会在那里。他们也可以放在/storage/emulated/1/DCIM中。因此最好让用户指定正确的目录。

09-27 11:02