我一直在尝试编写使用原生Android相机拍摄照片然后显示照片的基本应用程序。当我尝试显示图片时,我写的应用程序不断崩溃。我甚至下载了一个我在互联网上找到的项目http://www.techrepublic.com/blog/android-app-builder/androids-camera-intent-makes-taking-pics-a-snap/,但即便是我的手机也崩溃了。我会在这里发布代码;我的手机有问题,我该如何解决?我在Galaxy S3上运行Android 4.1.1
package com.authorwjf.camera;
import java.io.File;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import com.authorwjf.camera.R;
public class Main extends Activity implements OnClickListener {
private static final int TAKE_PICTURE = 0;
private Uri mUri;
private Bitmap mPhoto;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
((Button) findViewById(R.id.snap)).setOnClickListener(this);
((Button) findViewById(R.id.rotate)).setOnClickListener(this);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case TAKE_PICTURE:
if (resultCode == Activity.RESULT_OK) {
getContentResolver().notifyChange(mUri, null);
ContentResolver cr = getContentResolver();
try {
mPhoto = android.provider.MediaStore.Images.Media.getBitmap(cr, mUri);
((ImageView)findViewById(R.id.photo_holder)).setImageBitmap(mPhoto);
} catch (Exception e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
}
}
@Override
public void onClick(View v) {
if (v.getId()== R.id.snap) {
Intent i = new Intent("android.media.action.IMAGE_CAPTURE");
File f = new File(Environment.getExternalStorageDirectory(), "photo.jpg");
i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
mUri = Uri.fromFile(f);
startActivityForResult(i, TAKE_PICTURE);
} else {
if (mPhoto!=null) {
Matrix matrix = new Matrix();
matrix.postRotate(90);
mPhoto = Bitmap.createBitmap(mPhoto , 0, 0, mPhoto.getWidth(), mPhoto.getHeight(), matrix, true);
((ImageView)findViewById(R.id.photo_holder)).setImageBitmap(mPhoto);
}
}
}
}
答案 0 :(得分:1)
你有没有提到在android manifest XML中使用系统相机的权限?