我有一个问题,在我的Android应用程序上创建一个目录并将文件保存到它.我正在使用这段代码来执行此操作:
String filename = "MyApp/MediaTag/MediaTag-"+objectId+".png";
File file = new File(Environment.getExternalStorageDirectory(), filename);
FileOutputStream fos;
fos = new FileOutputStream(file);
fos.write(mediaTagBuffer);
fos.flush();
fos.close();
Run Code Online (Sandbox Code Playgroud)
但这是一个例外:
java.io.FileNotFoundException:/mnt/sdcard/MyApp/MediaCard/MediaCard-0.png(没有这样的文件或目录)
在那条线上: fos = new FileOutputStream(file);
如果我将文件名设置为:"MyApp/MediaTag-"+objectId+"它正在工作,但如果我尝试创建并将文件保存到另一个目录,则会抛出异常.那么任何想法我做错了什么?
还有一个问题:有没有办法让我的文件在外部存储中保密,这样用户就无法在图库中看到它们,只有当他连接他的设备时Disk Drive?
基本上,我想要做的是让用户自己创建folder,然后转到activity包含a button来启动它camera.
从那里我希望能够启动camera并将camera图像保存到新创建的文件夹中.
我在将camera图像保存到新创建的文件夹的最后一部分时遇到了问题.
这是我的Code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
EditText text = (EditText)findViewById(R.id.editText1);
EditText text2 = (EditText)findViewById(R.id.editText2);
@Override
public void onClick(View v) {
final String name = text.getText().toString();
final String placeName = text2.getText().toString();
String place = placeName.substring(0,3);
String direct = name + place ;
File folder = new File("/sdcard/CameraTest/" + direct + "/");
folder.mkdirs(); …Run Code Online (Sandbox Code Playgroud) 我有一个程序通过Intent打开相机拍照.那部分工作已经很好了.但是,我希望它保存到具有特定文件名的某个文件夹(文件名是可选的,但它确实非常有帮助).
所以这就是我到目前为止所拥有的.
这是打开相机的代码行:
//TODO camera stuff.
Intent openCam = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//The two lines of code below were commented out at first.
//They were eventually added when I tried to save it with a custom name and destination
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
openCam.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
startActivityForResult(openCam, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
Run Code Online (Sandbox Code Playgroud)
结果处理程序在这里:
//TODO handle result
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// Image captured and saved to fileUri …Run Code Online (Sandbox Code Playgroud)