0

I have code to show capture image from camera device like this

Intent intent    = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    String file_name = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date());
                    File file        = new File(Environment.getExternalStorageDirectory(),
                                        "tmp_avatar_" + file_name + ".jpg");
                    mImageCaptureUri = Uri.fromFile(file);
                    intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);

                    try {   

                        intent.putExtra("return-data", true);
                        intent.putExtra("mImageCaptureUri", mImageCaptureUri); 
                        startActivityForResult(intent, PICK_FROM_CAMERA);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }  

and set image bitmap from path , it works when I capture image from portrait view camera device , but when I capture image from landscape view camera its getting error , I think its because my activity to retrieve image is portrait . So can u give me advice in order to I can capture image from portrait or landscape view camera?? thanks

Ram kiran Pachigolla
  • 20,897
  • 15
  • 57
  • 78
ya Lya
  • 389
  • 3
  • 8
  • 23

2 Answers2

1

try this

at first start intent

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
startActivityForResult(cameraIntent, CAMERA_REQUEST); 

and then activity for result

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    if (requestCode == CAMERA_REQUEST) {  
        if(data!=null && resultCode == RESULT_OK)
        {
        bitMapForProfilePic = (Bitmap) data.getExtras().get("data");
        bitMapForProfilePic =Bitmap.createScaledBitmap(bitMapForProfilePic, getWindowManager().getDefaultDisplay().getWidth()/5, getWindowManager().getDefaultDisplay().getHeight()/4, true);
        registration_profilePicID.setImageBitmap(bitMapForProfilePic);           bitMapForProfilePic=null;
        }
      }
    }  

if u want to store the bitmap

    extStorageDirectory = Environment.getExternalStorageDirectory().toString();
    OutputStream outStream = null;
   File file = new File(extStorageDirectory, "profilepicture.PNG");
   try {
    outStream = new FileOutputStream(file);
    bitMapForProfilePic.compress(Bitmap.CompressFormat.PNG, 100, outStream);

    outStream.flush();
    outStream.close();


   } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }

then set the image

view.setImageBitmap(BitmapFactory.decodeFile("/sdcard/profilepicture.PNG"));

if u have any doubts let me know...

Thirupathig
  • 166
  • 9
1

I've faced the similar problem in one of my apps, I've fixed it in the following way: First save your instance state:

@Override
protected void onSaveInstanceState(Bundle state) {
    super.onSaveInstanceState(state);
state.putString(IMAGE_FILE_PATH, imageFilePath); }

And then restore it:

@Override
protected void onRestoreInstanceState(Bundle state) {
    super.onRestoreInstanceState(state);
ImageFilePath = state.getString(IMAGE_FILE_PATH); }

That works for me, but variable ImageFilePath should be global for your Activity so maybe there is more graceful way to achieve this

Chaosit
  • 1,116
  • 7
  • 21