我正在尝试开发一个应用程序,它涉及捕获图像,裁剪图像并将其保存到应用程序数据库。在这个过程中,我遇到了一个错误,这似乎很常见,但不清楚:
'意图' ' onActivityResult'中的参数method为null。
以下是活动代码:
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ScanActivity extends Activity {
ImageView imgFavorite;
//keep track of camera capture intent
final int CAMERA_CAPTURE = 1;
//captured picture uri
private Uri picUri;
//keep track of cropping intent
final int PIC_CROP = 2;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scan);
imgFavorite = (ImageView)findViewById(R.id.imageView1);
try {
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_CAPTURE);
}
catch(Exception e){
String errormesage="Oops-your device doesnt support capturing images!";
Toast.makeText(ScanActivity.this,errormesage,Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
ContentResolver cr=getContentResolver();
String imageName = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date());
File photo=new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),imageName);
Uri selectedImage=Uri.fromFile(photo);
Bitmap bp ;
try{
//user is returning from capturing an image using the camera
if (resultCode == RESULT_OK && requestCode == CAMERA_CAPTURE) {
picUri=data.getData();
//carry out the crop operation
performCrop(picUri);
bp = MediaStore.Images.Media.getBitmap(cr, selectedImage);
imgFavorite.setImageBitmap(bp);
Toast.makeText(ScanActivity.this,"Captured Successfully",Toast.LENGTH_SHORT).show();
}
else if (requestCode==PIC_CROP)
{
if(data!=null) {
//get the returned data
Bundle extras = data.getExtras();
//get the cropped bitmap
Bitmap thePic = extras.getParcelable("data");
//retrieve a reference to the ImageView
ImageView picView = (ImageView) findViewById(R.id.imageView1);
//display the returned cropped image
picView.setImageBitmap(thePic);
// open();
//1 Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
//startActivityForResult(intent, 0);
//1 startActivityForResult(intent,1);
}
}
else if(resultCode == RESULT_CANCELED){
Toast.makeText(getApplicationContext(),
"User cancelled image capture", Toast.LENGTH_SHORT).show();
}
}catch(Exception e){
Log.e(logtag,e.toString());
String errormesage="scan failed";
Toast.makeText(ScanActivity.this,errormesage,Toast.LENGTH_SHORT).show();
}
}
private void performCrop(Uri picUri){
try {
//call the standard crop action intent (the user device may not support it)
Intent cropIntent = new Intent("com.android.camera.action.CROP");
System.out.println("Past intent");
//indicate image type and Uri
cropIntent.setDataAndType(picUri, "image/*");
//set crop properties
cropIntent.putExtra("crop", "true");
//indicate aspect of desired crop
cropIntent.putExtra("aspectX", 2);
cropIntent.putExtra("aspectY", 1);
//indicate output X and Y
cropIntent.putExtra("outputX", 128);
cropIntent.putExtra("outputY", 128);
//retrieve data on return
cropIntent.putExtra("return-data", true);
//start the activity - we handle returning in onActivityResult
startActivityForResult(cropIntent, PIC_CROP);
System.out.println("Past start activity for intent");
}
catch(Exception e){
//display an error message
String errorMessage = "oops - your device doesn't support the crop action!";
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
System.out.println("exception message is :"+e.getMessage());
}
}