0

我试图保存由相机意图拍摄的图像。我的代码没有工作,所以当我添加检查是否创建了目录时,它应该没有。可能是什么问题?我在模拟器上试试。

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

我在日志中得到了上述内容。

下面是我在打开相机的按钮上的代码

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


            }
4

2 回答 2

1

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. newfile is the only file you care about in this code snippet, so call it file.

于 2013-01-11T09:25:50.117 回答
0

替换这一行

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

有了这个。

File file = new File (sdCard.getAbsolutePath()+"/YourDirectoryName");
于 2013-01-11T09:24:19.467 回答