我制作了一个应用程序,您可以使用默认相机拍摄照片,然后将其显示在图像视图上。问题是,图像不会显示在图像视图中。尝试了许多方法,但没有解决方案。
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>
答案 0 :(得分:1)
问题出在这里:
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
您正在使用requestCode ACTIVITY_START_CAMERA_APP
startActivityForResult(callCameraApplicationIntent, ACTIVITY_START_CAMERA_APP);
所以你需要修改if条件,如:
if (requestCode == ACTIVITY_START_CAMERA_APP && resultCode == RESULT_OK) {
答案 1 :(得分:1)
嗯,它不知道,因为您传递的捕获和活动结果的ID不同,startActivityForResult(callCameraApplicationIntent, ACTIVITY_START_CAMERA_APP);
来自requestCode == REQUEST_IMAGE_CAPTURE
匹配。
答案 2 :(得分:1)
在这里调试:也许它不会插入这个:
@RequestMapping(value = "ajaxhai.do", method = RequestMethod.POST,consumes="application/json")**//Please note : here it is String and not Object[]**
public String testAjaxPost(@RequestBody String ajaxRes, Model model,
HttpServletRequest request, HttpServletResponse response) {
System.out.println(ajaxRes);
Gson gson = new Gson();
TestAjaxDto[] mcArray = gson.fromJson(ajaxRes, TestAjaxDto[].class);
List<TestAjaxDto> mcList = new ArrayList<TestAjaxDto>(Arrays.asList(mcArray));
System.out.println(mcList);
return "ajaxform";
}
顺便说一下某些设备没有给图像内部数据。所以你必须在拍摄前给出路径。这是样本:
首先在课堂内添加:
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
mPhotoCapturedImageView.setImageBitmap(imageBitmap);
}
然后在课堂内的某处添加:
你必须存储它,因为相机是新意图
private Uri uriFilePath;
在onCreate上添加:
@Override
protected void onSaveInstanceState(Bundle outState) {
if (uriFilePath != null) outState.putString("uri_file_path", uriFilePath.toString());
super.onSaveInstanceState(outState);
}
这里是捕获功能:
if (savedInstanceState != null) {
if (uriFilePath == null && savedInstanceState.getString("uri_file_path") != null) {
uriFilePath = Uri.parse(savedInstanceState.getString("uri_file_path"));
}
}
然后使用它来获取onActivityResult中的图像:
private void captureImage() {
PackageManager packageManager = this.getPackageManager();
if (packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
File mainDirectory = new File(Environment.getExternalStorageDirectory(), "Tofaş");//tmp
if (!mainDirectory.exists())
mainDirectory.mkdirs();
Calendar calendar = Calendar.getInstance();
cameraFileName = "IMG_" + calendar.getTimeInMillis()+".jpg";
uriFilePath = Uri.fromFile(new File(mainDirectory, cameraFileName));
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uriFilePath);
startActivityForResult(intent, RC_CAMERA);
}
}