我正在尝试使用 MediaStore 拍照,将图像保存到特定文件夹并显示它。文件夹已创建,但图像文件未创建。
public void getPicture(View view)
{
File folder = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/DCIM/Camera/folder");
if(!folder.exists()) {
if(!folder.mkdirs()){
Toast.makeText(this, "cannot create folder", Toast.LENGTH_LONG).show();
}
}
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File imageFile = new File(folder, "image.jpg");
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imageFile));
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
startActivityForResult(cameraIntent, RESULT_CAMERA);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
setContentView(R.layout.activity);
if(resultCode == Activity.RESULT_OK)
{
if(requestCode == RESULT_CAMERA)
{
Bitmap bitmap = BitmapFactory.decodeFile(REAR_END_PATH + "/image.jpg");
// create byte array from the Bitmap object
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 20, stream);
ImageView iv = new ImageView(this);
iv.setImageBitmap(bitmap);
setContentView(iv);
}
}
}