【问题标题】:Android Camera - Save image into a new folder in SD CardAndroid 相机 - 将图像保存到 SD 卡中的新文件夹中
【发布时间】:2011-12-21 11:07:26
【问题描述】:

我有一个非常简单的应用程序,它现在拍照然后保存图像。目前的问题是,由于某种原因,我无法在手机上找到图像的保存位置。

我正在尝试做的最终结果是,当拍摄照片时,图像会保存到 SD 卡上创建的新文件夹中,但如果该文件夹不存在,则必须是在保存图像之前(自动)制作。

我曾尝试使用答案in this question,但似乎无法在没有收到错误imageIntent cannot be resolved 的情况下将其纳入其中

编辑:图像现在保存到 SD 卡并创建文件夹,但会覆盖以前的图像如果有人有任何建议代码已更新,我需要它来保存多个图像

这是我的代码的 sn-p:

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);`中的“图像”;

标签: android android-camera


【解决方案1】:

我纯粹是猜测你忘记了你链接到的问题部分的必要代码。

问题的最顶部有以下一行:

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

上面的代码 - 虽然未经个人测试 - 应该可以工作。

【讨论】:

  • 感谢您指出愚蠢的错误;)。如果您可以看一下,我刚刚更新了问题,现在每次我拍摄新图像时都会覆盖以前的图像。
  • 是的。我在上面发布的代码应该用File image = new File(imagesFolder, "image_001.jpg"); 替换您当前创建文件的方式。在您的类主体中(就像在任何方法之外),您必须输入int imageNum = 0;,然后我在上面发布的代码应该可以工作。如果这不能解决问题,您会遇到什么错误?
【解决方案2】:
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 卡上的新文件夹中
  • 这在 android 4.4 中不起作用。 onActivityForResult 中的意图为空。 Android 有什么变化吗?
【解决方案3】:

您可以保存多个图像。每次拍摄文件夹正在重新创建的照片时,您都会犯一个错误。

所以你必须检查文件夹是否已经存在。你只需要添加一行代码

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-12
    相关资源
    最近更新 更多