在自定义文件夹的Android保存相机图像 [英] Android Save Camera Image in Custom Folder
问题描述
我知道这个问题似乎重复比对这里一些其他问题<一href=\"http://stackoverflow.com/questions/8402378/how-to-save-image-captured-with-camera-in-specific-folder\">How保存在特定的文件夹 [像这样]用相机拍摄的图像,但我仍然有麻烦。我想要做的是使一个应用程序,你拍照与摄像头后,图像将被保存到一个新的文件夹(应用程序名称后标题)。我看到该文件夹的创建,但图像似乎没有被插入到他们。下面是我的一些code,我相信我搞砸了的。任何帮助那里将是一个很大的帮助。谢谢!
camera.setOnClickListener(新View.OnClickListener(){
公共无效的onClick(视图v){
// TODO自动生成方法存根
I =新意图(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(ⅰ,cameraData);
}
});
} 私人无效的SaveToFile(字符串消息)抛出异常{
字符串文件路径= getFilesDir()+;
档案文件=新的文件(文件路径+/ SD卡/ DCIM / 100MEDIA / Wardobe);
FileOutputStream中出=新的FileOutputStream(文件,真正的);
out.write(message.getBytes());
out.close();
saveImage(文件路径,/sdcard/DCIM/100MEDIA/Wardobe/image.jpg,BMP);
如果(battleborn!= NULL){
saveImage(文件路径,SD卡/ DCIM / 100MEDIA /衣柜/ image.jpg文件,BMP);
} }
公共无效saveImage(字符串路径,字符串目录,位图图像){
尝试{
FOS的FileOutputStream =新的FileOutputStream(路径+方向);
的BufferedOutputStream流=新的BufferedOutputStream(FOS);
bmp.com preSS(比较pressFormat.JPEG,50,流);
stream.flush();
stream.close();
}
赶上(FileNotFoundException异常五){
e.printStackTrace();
}
赶上(IOException异常五){
e.printStackTrace();
}
}
尝试使用这种
文件f =新的文件(Environment.getExternalStorageDirectory()
档案文件=新的文件(环境
.getExternalStorageDirectory()
+文件分割符
+/ your_folder_name /+.PNG); FOS的FileOutputStream = NULL;
尝试{
FOS =新的FileOutputStream(文件);
如果(FOS!= NULL){
bitmap.com preSS(Bitmap.Com pressFormat.PNG,90,FOS);
fos.close();
}
}
不要忘了允许添加到您的mainfest文件。
&LT;使用许可权的android:NAME =android.permission.WRITE_EXTERNAL_STORAGE/&GT;
I know this question may seem repetitive as compared to a few other questions on here How to save image captured with camera in specific folder[like this] but I am still having trouble. What I'm trying to do is make an app that after you take a picture with the camera, the image will be saved into a new folder (titled after the app name). I am seeing that the folder was created, but the images don't seem to be inserting into them. The following is some of my code where I believe I'm screwing up. Any help out there would be a big help. Thank you!
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();
}
}
Try using this
File f = new File(Environment.getExternalStorageDirectory()
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();
}
}
Don't forget to add permission to your mainfest file.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
这篇关于在自定义文件夹的Android保存相机图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!