0

I have a button to open the gallery and i have another one to open the phone's camera, when the image is loaded on a imageview it show a textview saying "image uploaded" and if its not loaded i use .seterror to shows a message "upload a picture" or something. I have tested this on different phones and it works on kitty kat and marshmallow however when i test it on a nougat cellphone, the picture doesn't seem to be attached, and i got the .seterror message, "upload a picture".

on manifest:

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

Code:

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

    if(requestCode == CAM_REQUEST && resultCode == Activity.RESULT_OK && data.getData() != null){
        Toast.makeText(TerminosYC.this.getActivity(), "si hay foto", Toast.LENGTH_LONG).show();
        //Bitmap bitmap = (Bitmap) data.getExtras().get("data");
        filePath = data.getData();
        try {
            bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), filePath);
            imageView.setImageBitmap(bitmap);

        } catch (IOException e) {
            e.printStackTrace();
        }
        //imageView.setImageBitmap(bitmap);
    }else if (requestCode == PICK_IMAGE_REQUEST && resultCode == Activity.RESULT_OK && data != null && data.getData() != null) {
        filePath = data.getData();
        try {
            bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), filePath);
            imageView.setImageBitmap(bitmap);

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

public String getPath(Uri uri) {
    Cursor cursor = getActivity().getContentResolver().query(uri, null, null, null, null);
    cursor.moveToFirst();
    String document_id = cursor.getString(0);
    document_id = document_id.substring(document_id.lastIndexOf(":") + 1);
    cursor.close();

    cursor = getActivity().getContentResolver().query(
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            null, MediaStore.Images.Media._ID + " = ? ", new String[]{document_id}, null);
    cursor.moveToFirst();
    String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
    cursor.close();

    return path;
}


 private void requestStoragePermission() {
    if (ContextCompat.checkSelfPermission(TerminosYC.this.getActivity(), android.Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)
        return;

    if (ActivityCompat.shouldShowRequestPermissionRationale(TerminosYC.this.getActivity(), android.Manifest.permission.READ_EXTERNAL_STORAGE)) {
        //If the user has denied the permission previously your code will come to this block
        //Here you can explain why you need this permission
        //Explain here why you need this permission
    }
    //And finally ask for the permission
    ActivityCompat.requestPermissions(TerminosYC.this.getActivity(), new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION_CODE);
}


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

    //Checking the request code of our request
    if (requestCode == STORAGE_PERMISSION_CODE) {

        //If permission is granted
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            //Displaying a toast
            Toast.makeText(TerminosYC.this.getActivity(), "Permission granted now you can read the storage", Toast.LENGTH_LONG).show();
        } else {
            //Displaying another toast if permission is not granted
            Toast.makeText(TerminosYC.this.getActivity(), "you just denied the permission", Toast.LENGTH_LONG).show();
        }
    }
}

 class btnTakePhotoClicker implements  Button.OnClickListener{

    @Override
    public void onClick(View view) {

 /*       if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA)
                != PackageManager.PERMISSION_GRANTED) {


            requestPermissions(new String[]{Manifest.permission.CAMERA},
                    1313);
        }*/

        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent,CAM_REQUEST);
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Daniel Prado
  • 79
  • 11
  • Please see: [How do I avoid misusing tags?](https://meta.stackoverflow.com/questions/354427/how-do-i-avoid-misusing-tags) – EJoshuaS - Stand with Ukraine Aug 04 '17 at 18:17
  • Use LogCat to examine the Java stack trace associated with your crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this Most likely, you will find that `filePath` is `null`, since `getData()` is supposed to be `null` in the response to `ACTION_IMAGE_CAPTURE`. – CommonsWare Aug 04 '17 at 18:18
  • Thanks @CommonsWare i will check that out – Daniel Prado Aug 04 '17 at 18:50

0 Answers0