【发布时间】:2017-04-17 12:39:06
【问题描述】:
我正在尝试捕获图像并在 imageView 中设置,就像 stackoverflow 中有人提供的这段代码一样,但我得到空白屏幕。 可能是什么问题呢? 谢谢。
private String pictureImagePath = "";
private void openBackCamera() {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = timeStamp + ".jpg";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
pictureImagePath = storageDir.getAbsolutePath() + "/" + imageFileName;
File file = new File(pictureImagePath);
Uri outputFileUri = Uri.fromFile(file);
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(cameraIntent, 1);
}
处理图片
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
File imgFile = new File(pictureImagePath);
if(imgFile.exists()){
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);
myImage.setImageBitmap(myBitmap);
}
}
}
【问题讨论】:
-
您是否在清单文件中添加了权限?
-
可能是位图太大而您的设备没有足够的内存?检查您的日志中的 BitmapToLargeException
-
您在哪个 Android 版本上进行测试?这不适用于牛轧糖及更高版本。
-
@Arunkumar,您的代码给了我错误:位图太大而无法上传到纹理中。当我尝试使用 Bitmap.createScaledBitmap 缩放位图时,图片变为横向模式。
标签: java android image-capture