我做了一個應用程序,您使用默認相機拍攝一張照片,然後將其顯示在圖像視圖上。問題是,圖像不顯示在圖像視圖中。嘗試了很多方法,但沒有解決方案。ImageView在活動中沒有顯示圖像
mainactivity.java:
public class MainActivity extends Activity {
private static final int ACTIVITY_START_CAMERA_APP = 0;
static final int REQUEST_IMAGE_CAPTURE = 1;
private ImageView mPhotoCapturedImageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPhotoCapturedImageView = (ImageView) findViewById(R.id.capturePhotoImageView);
}
public void takePhoto(View view){
Intent callCameraApplicationIntent = new Intent();
callCameraApplicationIntent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(callCameraApplicationIntent, ACTIVITY_START_CAMERA_APP);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Toast.makeText(this, "Picture taken sucessfully!", Toast.LENGTH_SHORT).show();
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
mPhotoCapturedImageView.setImageBitmap(imageBitmap);
}
}
}
main_activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.taufiq.ocrdemo.MainActivity">
<ImageView
android:id="@+id/capturePhotoImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/photoButton"
android:layout_marginBottom="19dp"
android:layout_marginEnd="43dp"
android:layout_marginLeft="43dp"
android:layout_marginRight="43dp"
android:layout_marginStart="43dp"
android:layout_marginTop="16dp"
android:contentDescription="@string/preview"
android:minHeight="300dp"
app:layout_constraintBottom_toTopOf="@+id/photoButton"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints"
tools:layout_constraintBottom_creator="1"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintTop_creator="1"
tools:minHeight="300dp" />
<Button
android:id="@+id/photoButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="takePhoto"
android:text="@string/capture_photo"
android:layout_weight="1"
tools:ignore="MissingConstraints"
tools:layout_constraintRight_creator="1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
tools:layout_constraintLeft_creator="1"
android:layout_marginBottom="27dp"
app:layout_constraintLeft_toLeftOf="parent" />
</android.support.constraint.ConstraintLayout>
以及你通過兩個不同的ID爲活動和圖像結果 – Remario