gpt4 book ai didi

java - 清空内部 DCIM 文件夹 - Android Java

转载 作者:行者123 更新时间:2023-12-01 12:32:53 25 4
gpt4 key购买 nike

嘿,我想清空内部文件夹“/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 中。因此最好让用户指示正确的目录。

关于java - 清空内部 DCIM 文件夹 - Android Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25806885/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com