Skip to content Skip to sidebar Skip to footer

Camera Intent Return Null Only On Some Devices

I am using camera to capture image and set the returned bitmap into an imageview but it is crashing on OnePlus devices. I checked on few htc devices and it is working perfectly. I

Solution 1:

What can be the problem here?

The problem is that you are calling getData() on the returned Intent. Returning a Uri is not part of the ACTION_MEDIA_STORE contract:

The caller may pass an extra EXTRA_OUTPUT to control where this image will be written. If the EXTRA_OUTPUT is not present, then a small sized image is returned as a Bitmap object in the extra field. This is useful for applications that only need a small image. If the EXTRA_OUTPUT is present, then the full-sized image will be written to the Uri value of EXTRA_OUTPUT.

Given that you did not put EXTRA_OUTPUT on the Intent you used with startActivityForResult(), the only result you will get is the Bitmap in the data extra.

If you want a full-size image written somewhere, provide a path to that location in EXTRA_OUTPUT, then hold onto that path and use it when onActivityResult() is called:

/***
 Copyright (c) 2008-2016 CommonsWare, LLC
 Licensed under the Apache License, Version 2.0 (the "License"); you may not
 use this file except in compliance with the License. You may obtain a copy
 of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
 by applicable law or agreed to in writing, software distributed under the
 License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
 OF ANY KIND, either express or implied. See the License for the specific
 language governing permissions and limitations under the License.

 From _The Busy Coder's Guide to Android Development_
 https://commonsware.com/Android
 */package com.commonsware.android.camcon;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import java.io.File;

publicclassCameraContentDemoActivityextendsActivity {
  privatestaticfinal String EXTRA_FILENAME=
    "com.commonsware.android.camcon.EXTRA_FILENAME";
  privatestaticfinal String FILENAME="CameraContentDemo.jpeg";
  privatestaticfinalint CONTENT_REQUEST=1337;
  private File output=null;

  @OverridepublicvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent i=newIntent(MediaStore.ACTION_IMAGE_CAPTURE);

    if (savedInstanceState==null) {
      File dir=
        Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);

      dir.mkdirs();
      output=newFile(dir, FILENAME);
    }
    else {
      output=(File)savedInstanceState.getSerializable(EXTRA_FILENAME);
    }

    if (output.exists()) {
      output.delete();
    }

    i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(output));

    startActivityForResult(i, CONTENT_REQUEST);
  }

  @OverrideprotectedvoidonSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    outState.putSerializable(EXTRA_FILENAME, output);
  }

  @OverrideprotectedvoidonActivityResult(int requestCode, int resultCode,
                                  Intent data) {
    if (requestCode == CONTENT_REQUEST) {
      if (resultCode == RESULT_OK) {
        Intent i=newIntent(Intent.ACTION_VIEW);

        i.setDataAndType(Uri.fromFile(output), "image/jpeg");
        startActivity(i);
        finish();
      }
    }
  }
}

Post a Comment for "Camera Intent Return Null Only On Some Devices"