我想拍照并将其保存为SD卡上的文件。一切正常,相机启动,拍照和保存。如果我检查我的设备上的图片文件夹,我看到拍摄的图片,但如果我从另一个actvity检查文件夹,我看不到拍摄的照片。同样,如果我从我的电脑上检查文件夹。我的代码出了什么问题?
以下是我的Cameraactivity代码
//Init ImageView
mPhotoCapturedImageView = (ImageView) findViewById(R.id.imgViewThumbNail);
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, ACTIVITY_START_CAMERA_APP);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == ACTIVITY_START_CAMERA_APP && resultCode == RESULT_OK) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
mPhotoCapturedImageView.setImageBitmap(thumbnail);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "IMAGE_" + timeStamp+ "_";
File file = new File(Environment.getExternalStorageDirectory()+ PHOTO_ALBUM + imageFileName + ".jpg");
try {
file.createNewFile();
FileOutputStream fo = new FileOutputStream(file);
//5
fo.write(bytes.toByteArray());
fo.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
PHOTO_ALBUM的路径是
public static final String PHOTO_ALBUM = "/MyIdea/IdeaGallery/";
答案 0 :(得分:0)
我发现SD卡在安装后正在扫描并显示所有新图片。这条简单的代码解决了我所有的问题:
MediaScannerConnection.scanFile(CameraActivity.this, new String[]{file.getPath()}, new String[]{"image/jpeg"}, null);
谢谢大家的帮助!