Android在自定义文件夹中保存相机图像

Bos*_*rus 4 directory camera android image save

我知道这个问题可能看似重复,与此处的一些其他问题相比如何保存用特定文件夹中的相机拍摄的图像 [像这样]但我仍然遇到麻烦.我要做的是制作一个应用程序,用相机拍照后,图像将保存到一个新文件夹(标题为应用程序名称后).我看到文件夹已创建,但图像似乎没有插入到它们中.以下是我的一些代码,我相信我搞砸了.任何帮助都会有很大的帮助.谢谢!

  camera.setOnClickListener(new View.OnClickListener() {                
            public void onClick(View v) {
                // TODO Auto-generated method stub
                i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(i, cameraData); 
            }
        });


    } 

    private void saveToFile(String message) throws Exception {
        String filePath = getFilesDir()+"";
        File file = new File(filePath + "/sdcard/DCIM/100MEDIA/Wardobe");
        FileOutputStream out = new FileOutputStream(file, true);
        out.write(message.getBytes());
        out.close();
        saveImage(filePath, "/sdcard/DCIM/100MEDIA/Wardobe/image.jpg", bmp);
        if(battleborn != null) {
            saveImage(filePath, "sdcard/DCIM/100MEDIA/Wardrobe/image.jpg", bmp);
        } 

    }
    public void saveImage(String path, String dir, Bitmap image) {
        try{
            FileOutputStream fos = new FileOutputStream(path + dir);
            BufferedOutputStream stream = new BufferedOutputStream(fos);
            bmp.compress(CompressFormat.JPEG, 50, stream);
            stream.flush();
            stream.close(); 
        }
        catch(FileNotFoundException e) { 
            e.printStackTrace();
        }
        catch(IOException e) {
            e.printStackTrace();
        }
    }
Run Code Online (Sandbox Code Playgroud)

Num*_*air 7

试试这个

File file = new File(Environment
    .getExternalStorageDirectory()
    + File.separator
    + "/your_folder_name/" + ".png");

FileOutputStream fos = null;
try {
  fos = new FileOutputStream(file);
  if (fos != null) {
    bitmap.compress(Bitmap.CompressFormat.PNG, 90, fos);
    fos.close();
  }
}
Run Code Online (Sandbox Code Playgroud)

不要忘记为mainfest文件添加权限.

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
Run Code Online (Sandbox Code Playgroud)