简体   繁体   English

使用Android相机拍照后,如何根据需要自动将图像保存到特定文件夹或创建新文件夹

[英]How to save an image automatically to a specific folder or create a new folder if needed after taking a picture using the camera for android

I am currently trying to create an app which when taking a picture using the camera, when saving the image, it saves to a specific folder location and if the folder doesn't currently exist on the phone, it creates the folder and saves the file to that location. 我当前正在尝试创建一个应用,该应用在使用相机拍摄照片时,在保存图像时将其保存到特定的文件夹位置,并且如果手机上当前不存在该文件夹,它将创建该文件夹并保存该文件到那个位置。 My code does not currently work, although I have tried. 尽管我已经尝试过,但是我的代码目前无法正常工作。 Could you please look at my code and advise me on what I need to do. 您能否看一下我的代码,并就我需要做的事情向我提出建议。

Here is the code: 这是代码:

else if(v==camera){
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        File newDirectory = new File(Environment.getExternalStorageDirectory() + "App_Pictures/");
        String filename = new String("image1.jpeg");
        File outputFile = new File(newDirectory, filename);
        outputFile.mkdirs();

        startActivity(intent);
        }

It looks like you're trying to get an external app to take a picture for you. 您似乎正在尝试获取外部应用来为您拍照。 If this is the case, you need to use startActivityForResult , not startActivity , because you want to receive the resulting photo. 在这种情况下,您需要使用startActivityForResult而不是startActivity ,因为您想接收生成的照片。 Then you will receive the result in the onActivityResult method of your activity. 然后,您将在活动的onActivityResult方法中收到结果。 This process is described in detail here . 此过程将在此处详细说明。

If you actually want your own app to take the picture, instead of an external app, you'll need to use a completely different approach. 如果您实际上希望自己的应用而不是外部应用来拍照,则需要使用完全不同的方法。 Here is an app that starts recording a video right when it is launched, and saves it to a directory on the SD card, so perhaps it's a useful starting point if you want to do it this way. 这是一个应用程序 ,可在启动时立即开始录制视频,并将其保存到SD卡上的目录中,因此如果您要这样做,可能是一个有用的起点。

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

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