我知道这个问题可能看似重复,与此处的一些其他问题相比How to save image captured with camera in specific folder [像这样],但我仍然遇到麻烦。我要做的是制作一个应用程序,用相机拍照后,图像将保存到一个新文件夹(标题为应用程序名称后)。我看到文件夹已创建,但图像似乎没有插入到它们中。以下是我的一些代码,我相信我搞砸了。任何帮助都会有很大的帮助。谢谢!
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();
}
}
答案 0 :(得分: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();
}
}
不要忘记为mainfest文件添加权限。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
答案 1 :(得分:1)
在调用Camera intent
之前尝试放置它Uri uriSavedImage=Uri.fromFile(new File("/sdcard/flashCropped.png"));
camera.putExtra("output", uriSavedImage);
startActivityForResult(camera, 1);