在某些版本的android中,Camera Intent不会返回上一个活动

时间:2015-05-24 20:08:46

标签: android android-4.4-kitkat android-4.2-jelly-bean android-camera-intent onactivityresult

我已经使用相机图标实现了许多图像视图,这样当用户触摸图标时它会启动相机并在拍摄照片时返回到调用相机意图的原始活动并在其中显示捕获的图像。 imageviews。

问题是这在我的Jellybean 4.2.2中可行。但是在一些有kitkat和Jellybean 4.2.1的手机中,当相机加载并拍摄照片时,它会提供保存或丢弃拍摄图像的选项(在我的手机中没有选择保存),当我点击保存时应用程序崩溃。有时甚至在我的手机中它会返回到之前的活动,但不显示捕获的图像。

如果可以,请帮助我。我发布了以下基本代码

ImageView photo1,photo2,photo3,photo4,photo5;
int photonum=0;
String image1,image2,image3,image4,image5;

// Activity request codes
private static final int CAMERA_CAPTURE_IMAGE_REQUEST_CODE = 100;
public static final int MEDIA_TYPE_IMAGE = 1;

// directory name to store captured images and videos
private static final String IMAGE_DIRECTORY_NAME = "MagicPlaces";

private Uri fileUri; // file url to store image/video


   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.data_uploadtab1);

    photo1= (ImageView) findViewById(R.id.photo1);
    photo2= (ImageView) findViewById(R.id.photo2);
    photo3= (ImageView) findViewById(R.id.photo3);
    photo4= (ImageView) findViewById(R.id.photo4);
    photo5= (ImageView) findViewById(R.id.photo5);

    photo1.setOnClickListener(this);
    photo2.setOnClickListener(this);
    photo3.setOnClickListener(this);
    photo4.setOnClickListener(this);
    photo5.setOnClickListener(this);
  }

   public void onClick(View v) {
    int id=v.getId();
    if(id==R.id.photo1){

      captureImage();
        photonum=1;

    }if(id==R.id.photo2){
      captureImage();
        photonum=2;
    }
    if(id==R.id.photo3){

        captureImage();
        photonum=3;
    }
    if(id==R.id.photo4){
        captureImage();
        photonum=4;

    }  if(id==R.id.photo5){

        captureImage();
        photonum=5;
    }

}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);
    switch(requestCode) {
           case(100):{
            if (resultCode == RESULT_OK) {
               String fileuri=data.getData().getPath();
               previewCapturedImage(fileuri);
            }
            else {
                // failed to capture image
                Toast.makeText(getApplicationContext(),
              "Sorry! Failed to capture image",Toast.LENGTH_SHORT)
                        .show();
            }
        }
        break;
    }
}

 private void previewCapturedImage(String fileUr) {
    try {
       // System.out.println(fileUr);
        // bimatp factory
        BitmapFactory.Options options = new BitmapFactory.Options();

        // downsizing image as it throws OutOfMemory Exception for larger
        // images
        options.inSampleSize = 16;

         Bitmap bitmap = BitmapFactory.decodeFile(fileUr,
                options);


        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 80, baos);
        byte[] b = baos.toByteArray();
        String encodedImage = Base64.encodeToString(b,Base64.DEFAULT);

        Log.e("LOOK", encodedImage);



        if(photonum==1){

        photo1.setImageBitmap(bitmap);
            image1=encodedImage;
    }
       if(photonum==2){
           photo2.setImageBitmap(bitmap);
           image2=encodedImage;
       }
        if(photonum==3){

            photo3.setImageBitmap(bitmap);
            image3=encodedImage;
        }
        if(photonum==4){

            photo4.setImageBitmap(bitmap);
            image4=encodedImage;
        }
        if(photonum==5){

            photo5.setImageBitmap(bitmap);
            image5=encodedImage;
        }

    } catch (NullPointerException e) {
        e.printStackTrace();
    }
}


    private void captureImage() {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
    //System.out.println(fileUri);

    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);



    // start the image capture Intent
    startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);

}

/**
 * Creating file uri to store image/video
 */
public Uri getOutputMediaFileUri(int type) {
    return Uri.fromFile(getOutputMediaFile(type));
}

/*
 * returning image / video
 */
private static File getOutputMediaFile(int type) {

    // External sdcard location
    File mediaStorageDir = new File(
            Environment
                    .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
            IMAGE_DIRECTORY_NAME);

    // Create the storage directory if it does not exist
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create "
                    + IMAGE_DIRECTORY_NAME + " directory");
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
            Locale.getDefault()).format(new Date());
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator
                + "IMG_" + timeStamp + ".jpg");
    }  else {
        return null;
    }

    return mediaFile;
}

0 个答案:

没有答案