【问题标题】:Android Save Camera Image in Custom FolderAndroid将相机图像保存在自定义文件夹中
【发布时间】:2012-12-07 10:15:10
【问题描述】:

How to save image captured with camera in specific folder[like this] 上的其他一些问题相比,我知道这个问题似乎是重复的,但我仍然遇到问题。我想做的是制作一个应用程序,在你用相机拍照后,图像将保存到一个新文件夹中(以应用程序名称命名)。我看到该文件夹​​已创建,但图像似乎没有插入其中。以下是我认为我搞砸的一些代码。任何帮助都会有很大的帮助。谢谢!

  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();
        }
    }

【问题讨论】:

  • Intent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage) ?
  • 不要硬编码 sdcard 路径。使用环境 .getExternalStorageDirectory()

标签: android camera save directory image


【解决方案1】:

试试这个

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();
  }
}

不要忘记为您的 mainfest 文件添加权限。

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

【讨论】:

  • 对不起,我还在为此苦苦挣扎。我确实在清单中获得了许可(我做对了!)但是我需要在哪里放置您的建议,我是否需要替换我的任何特色代码?我尝试将它添加到 SaveToFile 和 saveImage 方法中,并替换我为每个方法拥有的代码,但仍然没有点击。
  • 看看这篇文章 (developer.android.com/guide/topics/media/…)。它还解释了如何将图像保存到 sdcard。
  • 谢谢!几天前就搞定了,谢谢你的帮助!
【解决方案2】:

尝试在调用相机意图之前放置它

Uri uriSavedImage=Uri.fromFile(new File("/sdcard/flashCropped.png"));
camera.putExtra("output", uriSavedImage);
startActivityForResult(camera, 1);

【讨论】:

    猜你喜欢
    • 2013-05-20
    • 1970-01-01
    • 1970-01-01
    • 2012-12-13
    • 2011-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多