1

I am trying to choose an image from device's gallery, crop it and then display it in an imageView. After I click on an image in image selector, the app quits with logcat message showing V/FA: Inactivity, disconnecting from the service. I am running a service in background to get data from firestore.

I have also tried using cropping libraries like https://github.com/ArthurHub/Android-Image-Cropper but the behaviour was same.

I also tried the following.

    FABfile.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Intent intent  = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(intent, PICK_IMAGE_REQUEST);
    }
});

protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null){
        mImageUri = data.getData();
        imageView.setImageURI(result.getUri());
    }
}

Please help to resolve this issue.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
User3912
  • 11
  • 3
  • This question doesn't seem to have anything to do with Firebase or Cloud Storage, so I'm removing those tags. – Doug Stevenson Jul 29 '19 at 22:02
  • "the app quits with logcat message showing V/FA: Inactivity, disconnecting from the service" -- that is not an error. [Examine Logcat](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) again and look for a stack trace. – CommonsWare Jul 29 '19 at 22:02
  • This (http://freetexthost.com/0ebcwpznu5) is my logcat. I could't find anything causing the problem. – User3912 Jul 30 '19 at 06:57

1 Answers1

0

Use these methods to choose image from gallery:

 public void showGallery() {
    if (hasPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
        loadGallery();
    } else {
        requestPermissionsSafely(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE);
    }
}


 private void loadGallery() {
    Intent choose = new Intent(Intent.ACTION_PICK,
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(choose, PICK_IMAGE_GALLERY);
}


  @Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {

        case PERMISSION_REQUEST_CODE:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                loadGallery();
            }
            break;
    }
}


  @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == PICK_IMAGE_GALLERY) {
        if (resultCode == Activity.RESULT_OK) {
        Uri selectedImage = data.getData();
        }
    }
}

And:

  @TargetApi(Build.VERSION_CODES.M)
public void requestPermissionsSafely(String[] permissions, int requestCode) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        requestPermissions(permissions, requestCode);
    }
}

@TargetApi(Build.VERSION_CODES.M)
public boolean hasPermission(String permission) {
    return Build.VERSION.SDK_INT < Build.VERSION_CODES.M ||
            checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED;
}
Ehsan Aminifar
  • 515
  • 4
  • 15