简体   繁体   English

将捕获的图像保存在SD卡上的特定文件夹中

[英]Save captured image in specific folder on sd card

I'm working with camera in android and I'm new to android camera. 我在Android中使用相机,我是Android相机的新手。 I've followed the tutorial from here 我从这里开始关注教程

Whenever I'll add External storage permission in android manifest, camera saves it in default directory without asking me and I want to save it in my own folder created in sd card. 每当我在android清单中添加外部存储权限时,相机会将其保存在默认目录中,而不会询问我,我想将其保存在我自己在SD卡中创建的文件夹中。 I'd searched a lot but couldn't find any useful link. 我搜索了很多但找不到任何有用的链接。 Please help, any help will be much appreciated. 请帮助,任何帮助将不胜感激。 Thank you :) 谢谢 :)

You can add this code in onActivityResult. 您可以在onActivityResult中添加此代码。 This will store your image in a folder named "YourFolderName" 这会将您的图像存储在名为“YourFolderName”的文件夹中

String extr = Environment.getExternalStorageDirectory().toString()
            + File.separator + "YourFolderName";
    File myPath = new File(extr, fileName);
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(myPath);
        bitMap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
        fos.flush();
        fos.close();
        MediaStore.Images.Media.insertImage(context.getContentResolver(),
                bitMap, myPath.getPath(), fileName);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

also need to set permission on Manifest 还需要设置Manifest的权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

camera saves it in default directory without asking me and I want to save it in my own folder created in sd card 相机将它保存在默认目录中而不询问我,我想将它保存在我在SD卡中创建的文件夹中

You can tell the camera where you would like the photo to be stored via EXTRA_OUTPUT , as is shown in the documentation's training module on ACTION_IMAGE_CAPTURE . 您可以通过EXTRA_OUTPUT告诉相机您希望照片的存储EXTRA_OUTPUT ,如ACTION_IMAGE_CAPTURE上的文档培训模块所示。

There is no requirement for all cameras to store images where you ask it to, though. 但是,并不要求所有摄像机都存储您要求的图像。 If the file that you ask for does not exist after the image is taken, you will need to fall back to your existing application logic. 如果在拍摄图像后您要求的文件不存在,则需要回退到现有的应用程序逻辑。

You can assign a path to write the image data to specific location like this: 您可以指定一个路径将图像数据写入特定位置,如下所示:

String fileName = "/mnt/sdcard/foldername";
FileOutputStream  fos = new FileOutputStream(fileName);
fos.write(data);

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

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