在查看图片时,Android应用会在手机上崩溃

时间:2013-07-31 22:35:51

标签: android image android-intent camera

我正在编写一个Android应用程序,在尝试加载我刚刚使用该应用程序拍摄的图像时会一直崩溃。我之前问过这个,但没有人回复我。上一个问题中的某个人告诉我要编写“logcat”中的内容,所以这里是

     07-31 18:27:56.692: E/AndroidRuntime(3803): FATAL EXCEPTION: main
     07-31 18:27:56.692: E/AndroidRuntime(3803): java.lang.RuntimeException:
     Unable to resume activity {com.authorwjf.camera/com.authorwjf.camera.Main}:
     java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, 
     request=0, result=-1, data=null} to activity 
     {com.authorwjf.camera/com.authorwjf.camera.Main}:  
     java.lang.NullPointerException

我还会发布我用过的代码;我从techrepublic的一篇文章中得到了它,所以我表面上认为它有效。

我不知道如何修复NullPointerException,但我很确定代码是正确的。这一切都编译了,我过去只是看过照片时遇到过类似的问题。我是初学者,所以任何建议都表示赞赏。 (再一次,我确保检查所有权限都没问题。)

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 个答案:

没有答案