2017-04-13 77 views
0

我試圖實現應用程序,幫助用戶捕捉照片和裁剪或選擇圖像和裁剪它,應用程序在Android 4.4中成功工作,但是當試圖在另一個設備上測試時5.1對話框出現在裁剪圖像後並選擇具有以下消息的保存選項unfortunately gallery has stopped 下面的代碼我如何開始裁剪。圖庫已停止

public void ImageCropFunction() { 
    try { 
     CropIntent = new Intent("com.android.camera.action.CROP"); 
     CropIntent.setDataAndType(uri, "image/*"); 
     CropIntent.putExtra("crop", "true"); 
     CropIntent.putExtra("scaleUpIfNeeded", true); 
     CropIntent.putExtra("return-data", true); 
     startActivityForResult(CropIntent, 1); 
    } catch (ActivityNotFoundException e) { 

    } 
    } 

,並從相機

public void ClickImageFromCamera() { 

    CamIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 

    file = new File(Environment.getExternalStorageDirectory(), 
     "file" + String.valueOf(System.currentTimeMillis()) + ".jpg"); 
    uri = Uri.fromFile(file); 
    CamIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, uri); 
    CamIntent.putExtra("return-data", true); 
    startActivityForResult(CamIntent, 0); 
    } 

,並從畫廊

public void GetImageFromGallery() { 

    GalIntent = new Intent(Intent.ACTION_PICK, 
     android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
    startActivityForResult(Intent.createChooser(GalIntent, "Select Image From Gallery"), 2); 
    } 

挑圖片拍攝照片,並要求運行使用

public void EnableRuntimePermission() { 
    if (ActivityCompat.shouldShowRequestPermissionRationale(StartUp.this, 
     Manifest.permission.CAMERA)) { 
     Toast.makeText(StartUp.this, "CAMERA permission allows us to Access CAMERA app", Toast.LENGTH_LONG).show(); 
    } else { 
     ActivityCompat.requestPermissions(StartUp.this, new String[]{ 
      Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, 
      Manifest.permission.READ_EXTERNAL_STORAGE,}, RequestPermissionCode); 
    } 
    } 

和GET結果一次許可選擇ima GE或請求允許

@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == 0 && resultCode == RESULT_OK) { 
     ImageCropFunction(); 
    } else if (requestCode == 2) { 
     if (data != null) { 
     uri = data.getData(); 
     ImageCropFunction(); 
     } 
    } else if (requestCode == 1) { 
     if (data != null) { 
     Bundle bundle = data.getExtras(); 
     Bitmap bitmap = bundle.getParcelable("data"); 
     this.bitmap = bitmap; 
     imageView.setImageBitmap(bitmap); 
     buttonDetect.setVisibility(View.VISIBLE); 
     } 
    } 
    } 

logcat的沒有表現出崩潰後的任何問題只發生表明,該

START INPUT: com.android.internal.policy.impl.PhoneWindow$DecorView 

誰能幫助?

+0

'catch(ActivityNotFoundException e){ }'?空的?將通常的e.printStackTrace()放入其中,並在其中向用戶顯示e.getMessage()的Toast()。現在你的用戶和你不知道發生了什麼。 – greenapps

+0

'並獲取選擇圖像或請求權限的結果'您無法使用onActivityResult獲取權限。如果你得到一個,它將不會被調用。改爲實現onRequestPermissionsResult()。 – greenapps

+0

@greenapps for catch(ActivityNotFoundException e){}''''''''''我嘗試它,但沒有出現問題和吐司也不會出現。 獲得許可我的意思是檢查權限是否授予 – Azak

回答

0

Android沒有Intent裁剪,但相同的設備支持裁剪和其他不支持。 我使用this library作物。解決這個問題。