1

i have a force close problem with camera in android 7 when i call cameraopen(); and when i picking an image from gallery i get error before cropping : "Editing is not supported"

app working perfectly on android 4.4.2 but on android 7 crashing.why?

i don't know what to do. please help me

my codes:

 @Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    String languageToLoad  = "en"; // your language
    Locale locale = new Locale(languageToLoad);
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    getActivity().getBaseContext().getResources().updateConfiguration(config,
            getActivity().getBaseContext().getResources().getDisplayMetrics());
    final View view = inflater.inflate(R.layout.ijad_forooshgah, container, false);



    int permissionCheck = ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA);
    //----------------------------------
    if(permissionCheck == PackageManager.PERMISSION_DENIED)
        RequestRuntimePermission();
return view;


}

private void GalleryOpen() {
    GalIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(Intent.createChooser(GalIntent,"Select Image from Gallery"),2);
}
private void CameraOpen() {
    CamIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    file = new File(Environment.getExternalStorageDirectory(),
            "file"+String.valueOf(System.currentTimeMillis())+".jpg");
    uri = Uri.fromFile(file);
    CamIntent.putExtra(MediaStore.EXTRA_OUTPUT,uri);
    CamIntent.putExtra("return-data",true);
    startActivityForResult(CamIntent,0);
}

private void CropImage() {

    try{
        CropIntent = new Intent("com.android.camera.action.CROP");
        CropIntent.setDataAndType(uri,"image/*");

        CropIntent.putExtra("crop","true");
        CropIntent.putExtra("outputX",180);
        CropIntent.putExtra("outputY",180);
        CropIntent.putExtra("aspectX",4);
        CropIntent.putExtra("aspectY",4);
        CropIntent.putExtra("scaleUpIfNeeded",true);
        CropIntent.putExtra("return-data",true);

        startActivityForResult(CropIntent,1);
    }
    catch (ActivityNotFoundException ex)
    {

    }

}`private void RequestRuntimePermission() {
    if(ActivityCompat.shouldShowRequestPermissionRationale(getActivity(),Manifest.permission.CAMERA))
        Toast.makeText(getActivity(),"CAMERA permission allows us to access CAMERA app",Toast.LENGTH_SHORT).show();
    else
    {
        ActivityCompat.requestPermissions(getActivity(),new String[]{Manifest.permission.CAMERA},RequestPermissionCode);
    }
}
 @Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch (requestCode)
    {
        case RequestPermissionCode:
        {
            if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
                Toast.makeText(getActivity(),"Permission Granted",Toast.LENGTH_SHORT).show();
            else
                Toast.makeText(getActivity(),"Permission Canceled",Toast.LENGTH_SHORT).show();
        }
        break;
    }
}
 @Override
public void onClick(View view) {
    switch (view.getId()){
        case R.id.imageToUpload:


            AlertDialog.Builder ab = new AlertDialog.Builder(getActivity());
            ab.setMessage("انتخاب عکس");
            ab.setPositiveButton("دوربین", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    CameraOpen();
                }
            });
            ab.setNegativeButton("گالری", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    GalleryOpen();
                }
            });
            ab.show();
break;

} }

i need help. thank you

Ehsan.F
  • 35
  • 7
  • 2
    [Android does not have a `CROP` `Intent`](https://commonsware.com/blog/2013/01/23/no-android-does-not-have-crop-intent.html). There are many [image cropping libraries for Android](https://android-arsenal.com/tag/45). Please use one. – CommonsWare Mar 13 '18 at 16:14
  • so why force close with camera in android 7 when i call cameraopen(); – Ehsan.F Mar 13 '18 at 21:36
  • 1
    Use LogCat and examine the Java stack trace associated with your crash. Note that `ACTION_IMAGE_CAPTURE` does not take a `return-data` extra, and that [you cannot use `Uri.fromFile()` on Android 7.0+](https://stackoverflow.com/questions/38200282/android-os-fileuriexposedexception-file-storage-emulated-0-test-txt-exposed). – CommonsWare Mar 13 '18 at 21:42

1 Answers1

1

this library is so useful for you

implementation 'com.yanzhenjie:album:2.1.3'

https://github.com/yanzhenjie/Album