清空内部DCIM文件夹 - Android Java

时间:2014-09-12 11:06:16

标签: java android eclipse camera storage

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

1 个答案:

答案 0 :(得分:1)

使用getExternalStoragePublicDirectory(DIRECTORY_DCIM),您可能会获得/storage/emulated/0/DCIM。但它永远不能保证图片会在那里。他们也可以在/storage/emulated/1/DCIM。所以最好让用户指出正确的目录。