2015-01-05 69 views
0

我正在使用Android手機上的相機拍攝圖像。拍攝圖像後,我將完整圖像作爲uri傳遞給作物意圖(com.android.camera.action.CROP)。但是,當作物意圖返回時,它會以縮略圖大小返回到可分段。我試圖將相機中的圖像保存在文件中,併發送文件的uri,但它不會在作物意圖中打開圖像。我怎樣才能從它發送回來的包裹中獲得我在作物意圖中裁切的全尺寸圖像?下面是一些我使用的代碼:從作物意圖Android全圖

public void onActivityResult(int requestCode, int resultCode, 
     Intent intent) { 
    if (resultCode != Activity.RESULT_CANCELED) { 
     Uri uri = null; 
     switch (requestCode) { 
     case FILE_SELECT_CODE: 
      if (intent.getData() != null) { 
       uri = intent.getData(); 
       cropSelectedPicture(uri); 
      } 
      break; 
     case PICTURE_SELECT_CODE: 
      uri = intent.getData(); 
      if (uri == null) { 
       Bundle bundle = intent.getExtras(); 
       Bitmap photo = null; 
       if (bundle != null) { 
        photo = bundle.getParcelable("data"); 
       } 
       finishChangeProfilePicture(photo); 
      } else { 
       cropSelectedPicture(uri); 
      } 
      break; 
     case FILE_CROP_CODE: 
      Bundle extras = intent.getExtras(); 
      Bitmap thePic = extras.getParcelable("data"); 
      uri = extras.getParcelable("uri"); 
      finishChangeProfilePicture(thePic); 
      break; 
     } 
    } 
    super.onActivityResult(requestCode, resultCode, intent); 
} 

public void showCamera() { 
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    intent.putExtra("crop", "true"); 
    intent.putExtra("outputX", pictureWidth); 
    intent.putExtra("outputY", pictureHeight); 
    intent.putExtra("aspectX", pictureWidth); 
    intent.putExtra("aspectY", pictureHeight); 
    intent.putExtra("scale", true); 
    intent.putExtra("return-data", true); 
    try { 
     cordova.startActivityForResult(
       this, Intent.createChooser(intent, "Take Picture"), 
       PICTURE_SELECT_CODE); 
    } catch (android.content.ActivityNotFoundException ex) { 
     // Potentially direct the user to the Market with a Dialog 
     Toast.makeText(context, "Please install a Camera application.", 
       Toast.LENGTH_SHORT).show(); 
    } 
} 

public void cropSelectedPicture(Uri uri) { 
    Intent intent = new Intent("com.android.camera.action.CROP"); 
    intent.setDataAndType(uri, "image/*"); 
    intent.putExtra("crop", "true"); 
    intent.putExtra("outputX", pictureWidth); 
    intent.putExtra("outputY", pictureHeight); 
    intent.putExtra("aspectX", pictureWidth); 
    intent.putExtra("aspectY", pictureHeight); 
    intent.putExtra("scale", true); 
    intent.putExtra("return-data", true); 

    PackageManager packageManager = context.getPackageManager(); 
    List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0); 
    if (activities.size() > 0) { 
     for (int i = 0; i < activities.size(); i++) { 
      String label = (String) activities.get(i).loadLabel(packageManager); 
      if (label.equals("Crop picture")) { 
       ActivityInfo activity = activities.get(i).activityInfo; 
       ComponentName name = new ComponentName (activity.applicationInfo.packageName, 
                 activity.name); 
       intent.setComponent(name); 
      } 
     } 
    } 

    try { 
     cordova.startActivityForResult(this, intent, FILE_CROP_CODE); 
    } catch (android.content.ActivityNotFoundException ex) { 
     Toast.makeText(context, "Please install a Cropping program", Toast.LENGTH_SHORT).show(); 
    } 
} 
+0

Android不具有''CROP' Intent':http://commonsware.com/blog/2013/01/23/no-android-does-not-have-crop-intent.html – CommonsWare

+0

@CommonsWare我已經看到你的博客文章發佈在這個問題上。我沒有看到任何建議 - 在你的文章的圖書館之外。當畫廊安裝時,不存在的裁剪意圖不起作用。但是,當它使用Google+照片應用程序時,它會發生分裂,並且什麼也沒有發生。 – KickingLettuce

+0

@KickingLettuce:「當畫廊安裝時不存在的裁剪意圖工作正常」 - 當設備製造商或ROM修改器未修改圖庫時,只要圖庫應用程序選擇維持此無證和不受支持的API,只要用戶選擇Gallery應用程序。所有設備上都不存在圖庫應用,取而代之的是Google+照片(如您注意的)或其他應用(根據設備製造商或ROM模特的要求)。 – CommonsWare

回答

1

好了,所以我要做的就是從相機意圖保存的URI,然後將圖像保存到意向出來的作物意圖時。我以前曾嘗試過這種方法,但它是將內部文件傳遞給應用程序,而不是內容解析程序。 (公平的警告:這是一個cordova項目,不是原生的android項目,所以如果你在Media.getBitmap行遇到問題,那麼刪除cordova.getActivity()部分,這應該使它工作)這是我改變了:

public void onActivityResult(int requestCode, int resultCode, 
     Intent intent) { 
    if (resultCode != Activity.RESULT_CANCELED) { 
     switch (requestCode) { 
     case FILE_SELECT_CODE: 
      if (intent.getData() != null) { 
       uri = intent.getData(); 
       cropSelectedPicture(); 
      } 
      break; 
     case PICTURE_SELECT_CODE: 
      uri = intent.getData(); 
      if (uri == null) { 
       Bundle bundle = intent.getExtras(); 
       Bitmap photo = null; 
       if (bundle != null) { 
        photo = bundle.getParcelable("data"); 
       } 
       finishChangeProfilePicture(photo); 
      } else { 
       cropSelectedPicture(); 
      } 
      break; 
     case FILE_CROP_CODE: 
      Bitmap theUriPic; 
      try { 
       theUriPic = Media.getBitmap(cordova.getActivity().getContentResolver(), uri); 
       finishChangeProfilePicture(theUriPic); 
      } catch (FileNotFoundException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      break; 
     } 
    } 
    super.onActivityResult(requestCode, resultCode, intent); 
} 

public void cropSelectedPicture() { 
    Intent intent = new Intent("com.android.camera.action.CROP"); 
    intent.setDataAndType(uri, "image/*"); 
    intent.putExtra("crop", "true"); 
    intent.putExtra("outputX", pictureWidth); 
    intent.putExtra("outputY", pictureHeight); 
    intent.putExtra("aspectX", pictureWidth); 
    intent.putExtra("aspectY", pictureHeight); 
    intent.putExtra("scale", true); 
    intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); 
    intent.putExtra("return-data", true); 

    PackageManager packageManager = context.getPackageManager(); 
    List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0); 
    if (activities.size() > 0) { 
     for (int i = 0; i < activities.size(); i++) { 
      String label = (String) activities.get(i).loadLabel(packageManager); 
      if (label.equals("Crop picture")) { 
       ActivityInfo activity = activities.get(i).activityInfo; 
       ComponentName name = new ComponentName (activity.applicationInfo.packageName, 
                 activity.name); 
       intent.setComponent(name); 
      } 
     } 
    } 

    try { 
     cordova.startActivityForResult(this, intent, FILE_CROP_CODE); 
    } catch (android.content.ActivityNotFoundException ex) { 
     Toast.makeText(context, "Please install a Cropping program", Toast.LENGTH_SHORT).show(); 
    } 
}