0

I have been getting the above crash with the emulator in Android Studio on 6.0 but no crash on my device with 6.0.1. The emulator sometimes launches the camera but it mostly crashes with nothing in logcat to point me in the right direction. Does anyone have any clues as to what might be happening here?

Also, here is an image of when it does acually go through. Image_capture_camera

private File createImageFile() throws IOException
{
    // Create an image file name
    String timeStamp = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss").format(new Date());
    String imageFileName = "" + timeStamp;
    File storageDir = getActivity().getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );

    Name = imageFileName;
    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = image.getAbsolutePath();
    return image;
}

private void dispatchTakePictureIntent()
{
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // Ensure that there's a camera activity to handle the intent
    if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null)
    {
        // Create the File where the photo should go
        File photoFile = null;
        try
        {
            photoFile = createImageFile();
        }
        catch (IOException ex)
        {
            // Error occurred while creating the File
        }
        // Continue only if the File was successfully created
        if (photoFile != null)
        {
            photoURI = FileProvider.getUriForFile(getActivity(),
                    "com.full.jusuf.snaphealth.fileprovider",
                    photoFile);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }
    }
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK)
    {

        final Uri uri = photoURI;
        uri_data.add(new Timeline_Model(uri.toString(), Name));

        //save data to firebase
        FirebaseStorage storage = FirebaseStorage.getInstance();
        StorageReference storageRef = storage.getReference().child("users").child(FirebaseAuth.getInstance().getCurrentUser().getUid());
        storageRef.child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child(Name).putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot)
            {
                String uri1  = taskSnapshot.getDownloadUrl().toString();
                FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
                DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();
                if (user != null)
                {
                    long Count = System.currentTimeMillis();
                    databaseReference.child("users").child(user.getUid()).child("image_uri").child("image" + Count).setValue(new Timeline_Model(uri1, Name));
                }
            }
        });

        PopulateGallery();
    }
}

Logcat:

08-08 02:22:26.576 17275-17330/com.full.jusuf.snaphealth V/FA: Recording user engagement, ms: 10526
08-08 02:22:26.576 17275-17330/com.full.jusuf.snaphealth V/FA: Using measurement service
08-08 02:22:26.576 17275-17330/com.full.jusuf.snaphealth V/FA: Connecting to remote service
08-08 02:22:26.580 17275-17330/com.full.jusuf.snaphealth D/FA: Logging event (FE): user_engagement(_e), Bundle[{firebase_event_origin(_o)=auto, engagement_time_msec(_et)=10526, firebase_screen_class(_sc)=MainActivity, firebase_screen_id(_si)=4121746325476785971}]
08-08 02:22:26.593 17275-17330/com.full.jusuf.snaphealth V/FA: Using measurement service
08-08 02:22:26.593 17275-17330/com.full.jusuf.snaphealth V/FA: Connection attempt already in progress
08-08 02:22:26.593 17275-17330/com.full.jusuf.snaphealth V/FA: Activity paused, time: 917202
08-08 02:22:26.611 17275-17330/com.full.jusuf.snaphealth D/FA: Connected to remote service
08-08 02:22:26.611 17275-17330/com.full.jusuf.snaphealth V/FA: Processing queued up service tasks: 2
08-08 02:22:26.678 17275-17357/com.full.jusuf.snaphealth D/EGL_emulation: eglMakeCurrent: 0x7f9ce78225e0: ver 3 1 (tinfo 0x7f9cdb3c2d40)
08-08 02:22:26.679 17275-17357/com.full.jusuf.snaphealth E/Surface: getSlotFromBufferLocked: unknown buffer: 0x7f9cdb6c53e0
jusuf
  • 21
  • 5

2 Answers2

0

As you probably know, Android 23 changed the permission policy entirely, so if you target 23+ (or latest, which you probably do) it is likely your problem is here

File storageDir = getActivity().getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
        imageFileName,  /* prefix */
        ".jpg",         /* suffix */
        storageDir      /* directory */
);

or here

Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

(read this: Android M Camera Intent + permission bug?)

Make sure you have

 < uses-permission android:name="android.permission.CAMERA" />

 < uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

in your manifest, then go to settings -> apps -> yourApp -> permissions and make sure Storage and Camera are checked. If your crash no longer reproduces with the manually set permissions, proceed on reading https://developer.android.com/training/permissions/requesting.html and add runtime permission requests to all the features your application uses.

Nick
  • 585
  • 4
  • 11
  • Thanks for the reply, but it didn't work. Setting the permissions manually did the same thing. Strangely enough, launching the camera app on the simulator works but shows the same graphical glitches as the image I linked above. – jusuf Aug 08 '17 at 07:54
  • might be some hardware issue then, create another emulator with randomly different settings and set the camera to emulated instead of webcamN you probably have now and retry. – Nick Aug 08 '17 at 08:04
  • I get the same issue on the new emulator with emulated camera. – jusuf Aug 08 '17 at 08:14
  • yet it doesn't reproduce on any real devices? remove all logcat filtering (Package, Tag) and watch all output, hardware crashes that don't have java/android stacktraces might be harder to spot. try to isolate the issue – Nick Aug 08 '17 at 08:18
0

I've struggled with this for a long time, and then saw that from Android API 23 and up you need to request Runtime Permissions, read Runtime Permissions in Android Documentation and it should solve your problem

Boris
  • 358
  • 2
  • 6
  • No luck with this either, It works fine on every device tested except a nexus 5 6.0.1 and the simulator on 6.0 api 23. I've tested on hardware including a s8 and note 5 running 6.0.1 and 7.0 and have no issues. I can't seem to track down the cause though. – jusuf Aug 08 '17 at 13:45