我尝试过一个成功捕获图像的相机应用程序,但它没有在ImageView中显示,请帮我解决它。 当我去拍照并选择复选标记按钮以使用我尝试将图像显示回应用程序上的ImageView时出错的图片。
这是代码
public class MainActivity extends AppCompatActivity {
ImageView result;
static final int REQEST_IMAGE_CAPTURE=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button click= (Button) findViewById(R.id.camera);
result= (ImageView) findViewById(R.id.imageView);
click.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if(takePictureIntent.resolveActivity(getPackageManager())!=null)
{
startActivityForResult(takePictureIntent,REQEST_IMAGE_CAPTURE);
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode==REQEST_IMAGE_CAPTURE && requestCode == RESULT_OK){
Bundle extras=data.getExtras();
Bitmap imageBitmap =(Bitmap)extras.get("data");
result.setImageBitmap(imageBitmap);
}
}
}
这是
的XML文件<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Capture Image"
android:id="@+id/camera"
android:layout_marginBottom="68dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:minWidth="300dp"
android:minHeight="300dp"
/>
感谢您提前帮助。
答案 0 :(得分:0)
首先,保证您在 AndroidManifest.xml 中拥有所有必需的权限:
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
尝试以下方法:
使用以下命令定义要存储图片的路径:
String path = Environment.getExternalStorageDirectory() + "/path/image.jpg";
1)使用以下方法调用摄像头:
protected void startCameraActivity() {
File file = new File(path);
Uri outputFileUri = Uri.fromFile(file);
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, 0);
}
2)捕捉相机的活动结果(无论用户是拍照还是取消)
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i("MakeMachine", "resultCode: " + resultCode);
switch (resultCode) {
case 0:
Log.i("PhotoApp", "User cancelled");
break;
case -1:
onPhotoTaken();
break;
}
}
3)实现以下方法来解析
中的结果 protected void onPhotoTaken() {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
Bitmap bitmap = BitmapFactory.decodeFile(path, options);
image.setImageBitmap(bitmap);
}