简体   繁体   English

在使用Camera Intent Android时未创建目录

[英]Directory not created while using Camera intent Android

Im trying to save the image taken by teh Camera intent. 我试图保存通过相机意图拍摄的图像。 My code wasnt working so when i added the check whether the directory is even made, it should that it wasnt.What can possibly be wrong? 我的代码无法正常工作,因此当我添加检查目录是否完整时,是否应该没有,可能是什么错误? Im trying on the emulator. 我正在模拟器上尝试。

01-11 13:50:28.946: D/file(1161):  file is /mnt/sdcard/M3
01-11 13:50:28.946: D/file(1161):  photo is /mnt/sdcard/M3/3_1.jpg

I get the above in the log. 我在日志中得到了以上内容。

Below is the code i have on the button which opens camera 以下是我打开相机按钮上的代码

File sdCard = Environment.getExternalStorageDirectory();
            File file = new File (sdCard.getAbsolutePath() , File.separator + "/M3");
            file.mkdirs();
            String name = e_id + "_" + (size+1)+ ".jpg";
            File newfile = new File(file,name);
            newfile.mkdir();

                Log.d("file"," file is " + file.toString());
                Log.d("file"," photo is " + newfile.toString());


            if(!file.mkdir())
            {
                Log.d("file"," not created ");
                }
            if(!newfile.mkdir())
            {
                Log.d("newfile"," not created ");
                }
            else
            {
                outputFileUri = Uri.fromFile(newfile);
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
                startActivityForResult(intent, TAKE_PICTURE);


            }

The issue is that you are treating the image file as a directory: 问题是您将图像文件视为目录:

newfile.mkdir();

Try: 尝试:

File sdCard = Environment.getExternalStorageDirectory();
File dir = new File(sdCard.getAbsolutePath(), File.separator + "/M3");
if (!dir.mkdirs())
{
    Log.e(TAG, "Failed to create directory " + dir);
    return false;    // Assuming this is in a method returning boolean
}
String filename = e_id + "_" + (size+1)+ ".jpg";
File file = new File(dir, filename);

Log.d(TAG, "dir is " + dir.toString());
Log.d(TAF, "file is " + file.toString());

outputFileUri = Uri.fromFile(file);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, TAKE_PICTURE);

Note: you need to name your variables better; 注意:您需要更好地命名变量; file isn't a file it's a directory. file不是文件,而是目录。 newfile is the only file you care about in this code snippet, so call it file . newfile是此代码段中您唯一关心的文件,因此将其称为file

replace this line 替换这条线

File file = new File (sdCard.getAbsolutePath() , File.separator + "/M3");

with this one. 这个。

File file = new File (sdCard.getAbsolutePath()+"/YourDirectoryName");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM