我正在android中构建一个简单的相机应用程序,它会在点击按钮时启动相机。然后,摄像机意图将单击一个图像,该图像稍后将显示在MainActivity中的ImageView中。 然而问题是我能够捕获图像,但是一旦捕获图像而不是显示图像,应用程序突然崩溃。
以下是该应用的代码:
MainActivity.java
package com.example.suzancruz.cameraapplicaton;
import android.content.Intent;
import android.graphics.Bitmap;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
static final int REQUEST_IMAGE_CAPTURE = 123;
ImageView myImageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//Launching the camera
public void launchCamera(View view){
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//Take a picture and pass results along to onActivityResult
startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
}
//If you want to return the image taken
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK){
//Get the photo
Bundle extras = data.getExtras();
Bitmap photo = (Bitmap) extras.get("data");
myImageView.setImageBitmap(photo);
}
}
}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.suzancruz.cameraapplicaton.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="launchCamera"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Take Picture"
/>
<ImageView
android:layout_width="300dp"
android:layout_height="300dp"
android:id="@+id/myImageView"
/>
</RelativeLayout>
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.suzancruz.cameraapplicaton">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<uses-feature android:name="android.hardware.camera2"></uses-feature>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
</application>
</manifest>
答案 0 :(得分:0)
试试这个`public void onTakeImg(){
values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "Image");
values.put(MediaStore.Images.Media.DESCRIPTION, "From your Camera");
imageUri = getActivity().getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_IMAGE_CAPTURE:
if (resultCode == RESULT_OK) {
try {
mBitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), imageUri);
} catch (Exception e) {
e.printStackTrace();
}
onImageAvailable(mBitmap, requestCode);
Img.setImageBitmap(mBitmap);
}
break;
}
}
void onImageAvailable(Bitmap imageBitmap, int requestCode) {
String output = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString() + "/.immigration/temp/";
File f = new File(output);
if (!f.exists()) {
f.mkdirs();
}
String filename = output + "Image.jpg";
File fileDelete = new File(filename);
if (fileDelete.exists()) {
if (fileDelete.delete()) {
System.out.println("file Deleted :" + filename);
} else {
System.out.println("file not Deleted :" + filename);
}
}
try {
FileOutputStream out = new FileOutputStream(filename);
Matrix matrix = new Matrix();
if (imageBitmap.getWidth() < imageBitmap.getHeight()) {
matrix.postRotate(90);
}
mBitmap = Bitmap.createBitmap(imageBitmap, 0, 0, imageBitmap.getWidth(), imageBitmap.getHeight(), matrix, true);
mBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.close();
Log.d("Ar", "Ole");
} catch (IOException e) {
e.printStackTrace();
Log.d("Ar", e.toString());
}
File fdelete = new File(imageUri.getPath());
if (fdelete.exists()) {
if (fdelete.delete()) {
System.out.println("file Deleted :" + imageUri.getPath());
} else {
System.out.println("file not Deleted :" + imageUri.getPath());
}
}
}`