我有一个非常简单的应用程序,目前它可以拍照并保存图像。但问题是我无法找到图像在手机上的保存位置。
当拍照时,我希望图像被保存到在SD卡上新创建的文件夹中,但如果该文件夹尚不存在,则必须先自动创建该文件夹,然后才能保存图像。
我尝试使用此问题中的答案,但似乎无法将其合并,会出现错误
编辑:图像现在保存在SD卡中并创建了文件夹,但是如果需要保存多张图片,则会覆盖先前的图片。如果有人有任何建议,请更新代码。
以下是我的示例代码:
当拍照时,我希望图像被保存到在SD卡上新创建的文件夹中,但如果该文件夹尚不存在,则必须先自动创建该文件夹,然后才能保存图像。
我尝试使用此问题中的答案,但似乎无法将其合并,会出现错误
imageIntent cannot be resolved
。编辑:图像现在保存在SD卡中并创建了文件夹,但是如果需要保存多张图片,则会覆盖先前的图片。如果有人有任何建议,请更新代码。
以下是我的示例代码:
PictureCallback myPictureCallback_JPG = new PictureCallback(){
public void onPictureTaken(byte[] arg0, Camera arg1) {
// TODO Auto-generated method stub
/*Bitmap bitmapPicture
= BitmapFactory.decodeByteArray(arg0, 0, arg0.length); */
int imageNum = 0;
Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "Punch");
imagesFolder.mkdirs(); // <----
String fileName = "image_" + String.valueOf(imageNum) + ".jpg";
File output = new File(imagesFolder, fileName);
while (output.exists()){
imageNum++;
fileName = "image_" + String.valueOf(imageNum) + ".jpg";
output = new File(imagesFolder, fileName);
}
Uri uriSavedImage = Uri.fromFile(image);
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
OutputStream imageFileOS;
try {
imageFileOS = getContentResolver().openOutputStream(uriSavedImage);
imageFileOS.write(arg0);
imageFileOS.flush();
imageFileOS.close();
Toast.makeText(AndroidCamera.this,
"Image saved: ",
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();
}};
编辑
代码已更新,现在可以将多个图像保存在SD卡上创建的新文件夹中。
Uri uriSavedImage = Uri.fromFile(image);
中,“image”是什么意思? - VikramV