1

I try to get picture from android camera using this code but I will get error would you please helping me in this implementation,Thanks in advance

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    //try{
  super.onActivityResult(requestCode, resultCode, data);
  if (requestCode == SELECT_IMAGE)
    if (resultCode == Activity.RESULT_OK && null != data) {

        Uri selectedImage = data.getData();

        String[] filePathColumn = { MediaStore.Images.Media.DATA };

        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        Log.d("filecolumn : ",filePathColumn.toString());
        String picturePath = cursor.getString(columnIndex);
        cursor.close();

        Intent i = new Intent(getApplicationContext(), CustomizeActivity.class);
         i.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
  mCurrentPhotoPath);
        i.putExtra("type", "album");
        i.putExtra("photo", picturePath);


        Log.d("getPath : ",picturePath);
        startActivity(i);
        overridePendingTransition( R.anim.slide_in_left, R.anim.slide_out_left );
        Log.d("photo : ",selectedImage.getPath());
      // TODO Do something with the select image URI
    }


  if(requestCode == CAMERA_PIC_REQUEST)
  {
      Log.v("camera", "cam");
      if (resultCode == Activity.RESULT_OK) {
          Log.v("camera", "result ok");  
          Intent i = new Intent(getApplicationContext(), CustomizeActivity.class);
          i.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
mCurrentPhotoPath);
          i.putExtra("type", "photo");
          i.putExtra("photo", mCurrentPhotoPath);
          startActivity(i);
          overridePendingTransition( R.anim.slide_in_left, R.anim.slide_out_left );
      }
  }
}

error log:

01-22 15:53:55.375: D/szipinf(810): Initializing inflate state
01-22 15:53:55.386: V/photo path(810): photoPath : null
01-22 15:53:55.386: W/System.err(810): java.lang.NullPointerException: file == null
01-22 15:53:55.386: W/System.err(810):  at java.io.FileInputStream.<init>(FileInputStream.java:76)
01-22 15:53:55.386: W/System.err(810):  at java.io.FileInputStream.<init>(FileInputStream.java:132)
01-22 15:53:55.386: W/System.err(810):  at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:362)
01-22 15:53:55.386: W/System.err(810):  at com.odacia.appcarnaval.CustomizeActivity.onCreate(CustomizeActivity.java:181)
01-22 15:53:55.386: W/System.err(810):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-22 15:53:55.386: W/System.err(810):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
android.intent.action.MEDIA_UNMOUNTED 
4

1 回答 1

0

试试这个相机代码,它对我有用,从 2.2 到 4.0.1。

      private static final int PICK_Camera_IMAGE = 2;
camera.setOnClickListener(new OnClickListener() {

        //
        public void onClick(View v) {

            String name = dateToString(new Date(), "yyyy-MM-dd-hh-mm-ss");
            destination = new File(Environment
                    .getExternalStorageDirectory(), name + ".jpg");

            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT,
                    Uri.fromFile(destination));
            startActivityForResult(intent, PICK_Camera_IMAGE);

        }
    });


protected void onActivityResult(int requestCode, int resultCode,
        Intent imageReturnedIntent) {
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
    Uri selectedImageUri = null;
    String filePath = null;
    switch (requestCode) {
         case PICK_Camera_IMAGE:
        if (resultCode == RESULT_OK) {

            Toast.makeText(Setting.this, "staRtttt", Toast.LENGTH_LONG)
                    .show();

            try {
                in = new FileInputStream(destination);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 10;
            imagePath = destination.getAbsolutePath();
            mimagepath.setText(imagePath);
        //  Toast.makeText(Setting.this, "" + imagePath + "",Toast.LENGTH_LONG).show();
            Bitmap bmp = BitmapFactory.decodeStream(in, null, options);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();

            // CompressFormat set up to JPG, you can change to PNG or
            // whatever you
            // want;

            bmp.compress(CompressFormat.JPEG, 100, bos);
            bmp.compress(CompressFormat.JPEG, 100, bos);
            // **********************
            // globaly..................................
            data = bos.toByteArray();

        } else if (resultCode == RESULT_CANCELED) {
            Toast.makeText(this, "Picture was not taken",
                    Toast.LENGTH_SHORT).show();
        }
        break;
 }
}
于 2013-01-23T05:06:53.583 回答