安卓相机 - 将照片保存到SD卡中的新文件夹中

4
我有一个非常简单的应用程序,目前它可以拍照并保存图像。但问题是我无法找到图像在手机上的保存位置。
当拍照时,我希望图像被保存到在SD卡上新创建的文件夹中,但如果该文件夹尚不存在,则必须先自动创建该文件夹,然后才能保存图像。
我尝试使用此问题中的答案,但似乎无法将其合并,会出现错误imageIntent cannot be resolved
编辑:图像现在保存在SD卡中并创建了文件夹,但是如果需要保存多张图片,则会覆盖先前的图片。如果有人有任何建议,请更新代码。
以下是我的示例代码:
PictureCallback myPictureCallback_JPG = new PictureCallback(){

    public void onPictureTaken(byte[] arg0, Camera arg1) {
        // TODO Auto-generated method stub
        /*Bitmap bitmapPicture 
            = BitmapFactory.decodeByteArray(arg0, 0, arg0.length);  */


        int imageNum = 0;
        Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        File imagesFolder = new File(Environment.getExternalStorageDirectory(), "Punch");
        imagesFolder.mkdirs(); // <----
        String fileName = "image_" + String.valueOf(imageNum) + ".jpg";
        File output = new File(imagesFolder, fileName);
        while (output.exists()){
            imageNum++;
            fileName = "image_" + String.valueOf(imageNum) + ".jpg";
            output = new File(imagesFolder, fileName);
        }
        Uri uriSavedImage = Uri.fromFile(image);
        imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);


        OutputStream imageFileOS;
        try {
            imageFileOS = getContentResolver().openOutputStream(uriSavedImage);
            imageFileOS.write(arg0);
            imageFileOS.flush();
            imageFileOS.close();

            Toast.makeText(AndroidCamera.this, 
                    "Image saved: ", 
                    Toast.LENGTH_LONG).show();

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        camera.startPreview();
    }};

编辑

代码已更新,现在可以将多个图像保存在SD卡上创建的新文件夹中。


Uri uriSavedImage = Uri.fromFile(image); 中,“image”是什么意思? - VikramV
3个回答

4
我猜您可能忘记了您链接的问题部分中必要的代码。
该问题在最顶部有以下这行代码:
Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

如果您只是从答案中复制了代码,那么您会遇到错误,因为答案中的代码没有包含imageIntent的实例化。
如果您需要任何进一步的帮助或者我说得完全错了,请告诉我。
更新(关于图像被覆盖的问题):
您目前正在使用"image_001.jpg"作为表示图像名称的字符串。 在您的类中设置一个变量int imageNum = 0;, 然后您需要使用while循环并增加图像编号 - 或者您可以根据时间创建一个不同的图像名称 - 这是另一种方法。
String fileName = "image_" + String.valueOf(imageNum) + ".jpg";
File output = new File(imagesFolder, fileName);
while (output.exists()){
    imageNum++;
    fileName = "image_" + String.valueOf(imageNum) + ".jpg";
    output = new File(imagesFolder, fileName);
}
//now save the file to the sdcard using output as the file

上面的代码 - 虽然我没有亲自测试过 - 应该可以工作。

谢谢指出那个愚蠢的错误;)我刚刚更新了问题,如果你能看一下,现在每次我拍新照片时都会覆盖之前的图片。 - Matt
是的。我上面发布的代码应该替换你目前使用的 File image = new File(imagesFolder, "image_001.jpg"); 创建文件的方式。在你的类体(比如任何方法之外),你必须放置 int imageNum = 0;,然后我发布的代码应该可以工作。如果这样还是不行,你得到了什么错误? - Reed

3
try {

   super.onCreate(savedInstanceState);
   File root = new File(Environment.getExternalStorageDirectory()
     + File.separator + "Your Floder Name"+ File.separator);
   root.mkdirs();
   sdImageMainDirectory = new File(root, "myPicName.jpg");
    outputFileUri = Uri.fromFile(sdImageMainDirectory);
   startCameraActivity();
  } catch (Exception e) {

   Toast.makeText(this, "Error occured. Please try again later.",
     Toast.LENGTH_SHORT).show();
   finish();
  }

 }

 protected void startCameraActivity() {



  Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
  cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
  startActivityForResult(cameraIntent, 101);


 }

 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);

  if(requestCode==101 && resultCode==-1)
  {
   try
   {
    Uri outputFileUri= data.getData();
    selectedImagePath=getPath(outputFileUri);


   }
   catch(Exception ex)
   {
    Log.v("OnCameraCallBack",ex.getMessage());


   }
  }

谢谢您的回答,但我已经更新了我的问题,我成功将其保存到了SD卡上的一个新文件夹中。 - Matt
这在Android 4.4中不起作用。onActivityForResult中的意图为null。Android发生了什么变化? - user798719

2

您可以保存多张图片。每次拍摄照片时,您都会犯一个错误,即文件夹一遍又一遍地重新创建。

所以,您需要检查文件夹是否已经存在。只需添加一行代码即可。

if (!imagesFolder.exists()) {
imagesFolder.mkdirs();
}
else { //do nothing}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接