我正在拍摄Picure,并将其保存在默认文件夹中。
这是我的PictureCallBack()
功能:
PictureCallback myPictureCallback_JPG = new PictureCallback(){
public void onPictureTaken(byte[] arg0, Camera arg1) {
Uri uriTarget = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, new ContentValues());
OutputStream imageFileOS;
try {
imageFileOS = getContentResolver().openOutputStream(uriTarget);
imageFileOS.write(arg0);
imageFileOS.flush();
imageFileOS.close();
Toast.makeText(MainActivity.this,"Image saved: " + uriTarget.toString(),Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
camera.startPreview();
}
};
如何将其保存在"/sdcard/MyCustonApp/Photos/"
等特定文件夹中?
答案 0 :(得分:4)
添加到您的代码
String path = Environment.getExternalStorageDirectory().toString();
File myNewFolder = new File(path + "/your/folder");
myNewFolder.mkdir();
File file = new File(path + "/your/folder", name + ".png");
OutputStream imageFileOS = new FileOutputStream(file);
答案 1 :(得分:1)
这是我如何检查文件夹是否要将捕获的图像保存到,存在或创建文件夹(如果不存在)。
Intent getCameraImage = new Intent("android.media.action.IMAGE_CAPTURE");
File cameraFolder;
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
cameraFolder = new File(android.os.Environment.getExternalStorageDirectory(),"MyCustomApp/Photos");
else
cameraFolder = StatusUpdate.this.getCacheDir();
if(!cameraFolder.exists())
cameraFolder.mkdirs();
File photo = new File(Environment.getExternalStorageDirectory(), "MyCustomApp/Photos/camera_snap.jpg");
getCameraImage.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
Uri uri = Uri.fromFile(photo);
startActivityForResult(getCameraImage, 1);
这段代码会将捕获的图像保存到指定文件夹中,名称为 camera_snap ,扩展名为: .jpg
注意:这要求您在Manifest
文件中声明以下权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
答案 2 :(得分:1)
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyImages");
imagesFolder.mkdirs();
File image = new File(imagesFolder, "image.jpg");
Uri uriSavedImage = Uri.fromFile(image);
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
看一下这个链接。 How to save images from Camera in Android to specific folder?
答案 3 :(得分:0)
尝试这个简单的代码并将图像存储在一个文件夹中:
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/req_images");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-" + n + ".jpg";
file = new File(myDir, fname);
Log.i(TAG, "" + file);
if (file.exists())
file.delete();
try {
FileOutputStream out = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
其中bm表示位图图像名称