Android 在CameraApplication上传递resultInfo失败

Android 在CameraApplication上传递resultInfo失败,android,eclipse,Android,Eclipse,我正在用我在互联网上找到的代码做一个摄像头应用程序,它可以捕获和裁剪图像。在我想将图像保存到SDd卡之前,一切正常。它意外停止并产生以下错误: 08-06 15:06:58.341: E/AndroidRuntime(26462): FATAL EXCEPTION: main 08-06 15:06:58.341: E/AndroidRuntime(26462): java.lang.RuntimeException: Failure delivering result ResultInfo{

我正在用我在互联网上找到的代码做一个摄像头应用程序,它可以捕获和裁剪图像。在我想将图像保存到SDd卡之前,一切正常。它意外停止并产生以下错误:

08-06 15:06:58.341: E/AndroidRuntime(26462): FATAL EXCEPTION: main

08-06 15:06:58.341: E/AndroidRuntime(26462): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=-1, data=Intent { (has extras) }} to activity {net.londatiga.android/net.londatiga.android.MainActivity}: java.lang.NullPointerException

08-06 15:06:58.341: E/AndroidRuntime(26462):    at android.app.ActivityThread.deliverResults(ActivityThread.java:2994)

08-06 15:06:58.341: E/AndroidRuntime(26462):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3037)

08-06 15:06:58.341: E/AndroidRuntime(26462):    at android.app.ActivityThread.access$1100(ActivityThread.java:128)

08-06 15:06:58.341: E/AndroidRuntime(26462):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1191)

08-06 15:06:58.341: E/AndroidRuntime(26462):    at android.os.Handler.dispatchMessage(Handler.java:99)

08-06 15:06:58.341: E/AndroidRuntime(26462):    at android.os.Looper.loop(Looper.java:137)

08-06 15:06:58.341: E/AndroidRuntime(26462):    at android.app.ActivityThread.main(ActivityThread.java:4514)

08-06 15:06:58.341: E/AndroidRuntime(26462):    at java.lang.reflect.Method.invokeNative(Native Method)

08-06 15:06:58.341: E/AndroidRuntime(26462):    at java.lang.reflect.Method.invoke(Method.java:511)

08-06 15:06:58.341: E/AndroidRuntime(26462):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)

08-06 15:06:58.341: E/AndroidRuntime(26462):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)

08-06 15:06:58.341: E/AndroidRuntime(26462):    at dalvik.system.NativeStart.main(Native Method)

08-06 15:06:58.341: E/AndroidRuntime(26462): Caused by: java.lang.NullPointerException

08-06 15:06:58.341: E/AndroidRuntime(26462):    at java.io.File.fixSlashes(File.java:185)

08-06 15:06:58.341: E/AndroidRuntime(26462):    at java.io.File.<init>(File.java:134)

08-06 15:06:58.341: E/AndroidRuntime(26462):    at net.londatiga.android.MainActivity.saveBitmapToFile(MainActivity.java:128)

08-06 15:06:58.341: E/AndroidRuntime(26462):    at net.londatiga.android.MainActivity.onActivityResult(MainActivity.java:112)

08-06 15:06:58.341: E/AndroidRuntime(26462):    at android.app.Activity.dispatchActivityResult(Activity.java:4746)

08-06 15:06:58.341: E/AndroidRuntime(26462):    at android.app.ActivityThread.deliverResults(ActivityThread.java:2990)

08-06 15:06:58.341: E/AndroidRuntime(26462):    ... 11 more
08-06 15:06:58.341:E/AndroidRuntime(26462):致命异常:主
08-06 15:06:58.341:E/AndroidRuntime(26462):java.lang.RuntimeException:未能将结果ResultInfo{who=null,request=2,result=-1,data=Intent{(has extras)}传递到活动{net.londatiga.android/net.londatiga.android.MainActivity}:java.lang.NullPointerException
08-06 15:06:58.341:E/AndroidRuntime(26462):在android.app.ActivityThread.deliverResults(ActivityThread.java:2994)上
08-06 15:06:58.341:E/AndroidRuntime(26462):在android.app.ActivityThread.handleSendResult(ActivityThread.java:3037)上
08-06 15:06:58.341:E/AndroidRuntime(26462):在android.app.ActivityThread.access$1100(ActivityThread.java:128)
08-06 15:06:58.341:E/AndroidRuntime(26462):在android.app.ActivityThread$H.handleMessage(ActivityThread.java:1191)
08-06 15:06:58.341:E/AndroidRuntime(26462):在android.os.Handler.dispatchMessage(Handler.java:99)上
08-06 15:06:58.341:E/AndroidRuntime(26462):位于android.os.Looper.loop(Looper.java:137)
08-06 15:06:58.341:E/AndroidRuntime(26462):位于android.app.ActivityThread.main(ActivityThread.java:4514)
08-06 15:06:58.341:E/AndroidRuntime(26462):位于java.lang.reflect.Method.Invokenactive(本机方法)
08-06 15:06:58.341:E/AndroidRuntime(26462):位于java.lang.reflect.Method.invoke(Method.java:511)
08-06 15:06:58.341:E/AndroidRuntime(26462):位于com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
08-06 15:06:58.341:E/AndroidRuntime(26462):位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
08-06 15:06:58.341:E/AndroidRuntime(26462):在dalvik.system.NativeStart.main(本机方法)
08-06 15:06:58.341:E/AndroidRuntime(26462):由以下原因引起:java.lang.NullPointerException
08-06 15:06:58.341:E/AndroidRuntime(26462):位于java.io.File.fixSlashes(File.java:185)
08-06 15:06:58.341:E/AndroidRuntime(26462):位于java.io.File.(File.java:134)
08-06 15:06:58.341:E/AndroidRuntime(26462):位于net.londatiga.android.MainActivity.saveBitmapToFile(MainActivity.java:128)
08-06 15:06:58.341:E/AndroidRuntime(26462):在net.londatiga.android.MainActivity.onActivityResult(MainActivity.java:112)上
08-06 15:06:58.341:E/AndroidRuntime(26462):在android.app.Activity.dispatchActivityResult(Activity.java:4746)上
08-06 15:06:58.341:E/AndroidRuntime(26462):在android.app.ActivityThread.deliverResults(ActivityThread.java:2990)上
08-06 15:06:58.341:E/AndroidRuntime(26462):。。。还有11个

代码:

package net.londatiga.android;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Path;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {
    private Uri mImageCaptureUri;
    private ImageView mImageView;
    private String path;
    private Bitmap bitmap;

    private static final int PICK_FROM_CAMERA = 1;
    private static final int CROP_FROM_CAMERA = 2;
    private static final int PICK_FROM_FILE = 3;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        final String [] items           = new String [] {"Take from camera", "Select from gallery"};                
        ArrayAdapter<String> adapter    = new ArrayAdapter<String> (this, android.R.layout.select_dialog_item,items);
        AlertDialog.Builder builder     = new AlertDialog.Builder(this);

        builder.setTitle("Select Image");
        builder.setAdapter( adapter, new DialogInterface.OnClickListener() {
            public void onClick( DialogInterface dialog, int item ) { //pick from camera
                if (item == 0) {
                    Intent intent    = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

                    mImageCaptureUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),
                                       "tmp_avatar_" + String.valueOf(System.currentTimeMillis()) + ".jpg"));

                    intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);

                    try {
                        intent.putExtra("return-data", true);

                        startActivityForResult(intent, PICK_FROM_CAMERA);
                    } catch (ActivityNotFoundException e) {
                        e.printStackTrace();
                    }
                } else { //pick from file
                    Intent intent = new Intent();

                    intent.setType("image/*");
                    intent.setAction(Intent.ACTION_GET_CONTENT);

                    startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_FILE);
                }
            }
        } );

        final AlertDialog dialog = builder.create();

        Button button   = (Button) findViewById(R.id.btn_crop);
        mImageView      = (ImageView) findViewById(R.id.iv_photo);

        button.setOnClickListener(new View.OnClickListener() {  
            public void onClick(View v) {
                dialog.show();
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != RESULT_OK) return;

        switch (requestCode) {
            case PICK_FROM_CAMERA:
                doCrop();

                break;

            case PICK_FROM_FILE: 
                mImageCaptureUri = data.getData();

                doCrop();

                break;          

            case CROP_FROM_CAMERA:          
                Bundle extras = data.getExtras();

                if (extras != null) {               
                    Bitmap photo = extras.getParcelable("data");
                    saveBitmapToFile("/sdcard/cropped_img.jpg", photo);

                    mImageView.setImageBitmap(photo);
                }

                File f = new File(mImageCaptureUri.getPath());            

                if (f.exists()) f.delete();

                break;
//
        }
    }

    private boolean saveBitmapToFile(String string, Bitmap photo) {

        File file = new File(path);
        boolean res = false; if (!file.exists())
        {
        try 
        {
        FileOutputStream fos = new FileOutputStream(file); 
        res = bitmap.compress(CompressFormat.JPEG, 100, fos); fos.close();
        } catch (Exception e) 
        {   }
        } return res;
        }

//  }

    private void doCrop() {
        final ArrayList<CropOption> cropOptions = new ArrayList<CropOption>();

        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setType("image/*");

        List<ResolveInfo> list = getPackageManager().queryIntentActivities( intent, 0 );

        int size = list.size();

        if (size == 0) {            
            Toast.makeText(this, "Can not find image crop app", Toast.LENGTH_SHORT).show();

            return;
        } else {
            intent.setData(mImageCaptureUri);

            intent.putExtra("outputX", 200);
            intent.putExtra("outputY", 200);
            intent.putExtra("aspectX", 1);
            intent.putExtra("aspectY", 1);
            intent.putExtra("scale", true);
            intent.putExtra("return-data", true);

            if (size == 1) {
                Intent i        = new Intent(intent);
                ResolveInfo res = list.get(0);

                i.setComponent( new ComponentName(res.activityInfo.packageName, res.activityInfo.name));

                startActivityForResult(i, CROP_FROM_CAMERA);
            } else {
                for (ResolveInfo res : list) {
                    final CropOption co = new CropOption();

                    co.title    = getPackageManager().getApplicationLabel(res.activityInfo.applicationInfo);
                    co.icon     = getPackageManager().getApplicationIcon(res.activityInfo.applicationInfo);
                    co.appIntent= new Intent(intent);

                    co.appIntent.setComponent( new ComponentName(res.activityInfo.packageName, res.activityInfo.name));

                    cropOptions.add(co);
                }

                CropOptionAdapter adapter = new CropOptionAdapter(getApplicationContext(), cropOptions);

                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("Choose Crop App");
                builder.setAdapter( adapter, new DialogInterface.OnClickListener() {
                    public void onClick( DialogInterface dialog, int item ) {
                        startActivityForResult( cropOptions.get(item).appIntent, CROP_FROM_CAMERA);
                    }
                });

                builder.setOnCancelListener( new DialogInterface.OnCancelListener() {
                    public void onCancel( DialogInterface dialog ) {

                        if (mImageCaptureUri != null ) {
                            getContentResolver().delete(mImageCaptureUri, null, null );
                            mImageCaptureUri = null;
                        }
                    }
                } );

                AlertDialog alert = builder.create();

                alert.show();
            }
        }
    }
}
package net.londatiga.android;
导入java.io.File;
导入java.io.FileOutputStream;
导入java.util.ArrayList;
导入java.util.List;
导入android.app.Activity;
导入android.app.AlertDialog;
导入android.content.ActivityNotFoundException;
导入android.content.ComponentName;
导入android.content.DialogInterface;
导入android.content.Intent;
导入android.content.pm.ResolveInfo;
导入android.graphics.Bitmap;
导入android.graphics.Bitmap.CompressFormat;
导入android.graphics.Path;
导入android.net.Uri;
导入android.os.Bundle;
导入android.os.Environment;
导入android.provider.MediaStore;
导入android.view.view;
导入android.widget.ArrayAdapter;
导入android.widget.Button;
导入android.widget.ImageView;
导入android.widget.Toast;
公共类MainActivity扩展了活动{
私有Uri mImageCaptureUri;
私有图像视图mImageView;
私有字符串路径;
私有位图;
私有静态最终整型从摄像机拾取=1;
来自摄像机的专用静态最终整数裁剪=2;
私有静态最终int PICK_FROM_FILE=3;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
最终字符串[]项=新字符串[]{“从相机拍摄”、“从多媒体资料中选择”};
ArrayAdapter=new ArrayAdapter(这是android.R.layout.select_dialog_items,items);
AlertDialog.Builder=新建AlertDialog.Builder(此);
builder.setTitle(“选择图像”);
setAdapter(适配器,新的DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int项){//pick from camera
如果(项==0){
意向意向=新意向(MediaStore.ACTION\u IMAGE\u CAPTURE);
mImageCaptureUri=Uri.fromFile(新文件(Environment.getExternalStorageDirectory(),
“tmp_头像”+String.valueOf(System.currentTimeMillis())+“.jpg”);
intent.putExtra(android.provider.MediaStore.EXTRA_输出,mImageCaptureUri);
试一试{
intent.putExtra(“返回数据”,true);
startActivityForResult(意图,从摄像机中拾取);
}捕获(ActivityNotFounde异常){
e、 printStackTrace();
}
}else{//从文件中拾取
意图=新意图();
intent.setType(“image/*”);
intent.setAction(intent.ACTION\u GET\u CONTENT);
startActivityForResult(Intent.createChooser(Intent,“使用完成操作”),从文件中选择;
}
}
} );
最终AlertDialog=builder.create();
按钮按钮=(按钮)findViewById(R.id.btn\u裁剪);
mImageView=(ImageView)findViewById(R.id.iv_照片);
button.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
dialog.show();
}
});
}
@凌驾