2017-06-12 170 views
0

每当我使用camera裁剪图像时,出现错误Unable to load image。但在gallery的情况下,它运行良好。在android中裁剪图像

Uri uriPath = StoreAndFetchImageFromFile.getInstance(ParentDetails.this).getImageUri(partFilename); 
       selectedimagepath = getPath(uriPath); 

       Bitmap myBitmap = BitmapFactory.decodeFile(selectedimagepath); 
       parentimage.setImageBitmap(myBitmap); 
       performCropCamera(uriPath); 

和方法imagecrop是:

private void performCropCamera(Uri picUri) { 
    // take care of exceptions 
    try { 
     // call the standard crop action intent (the user device may not 
     // support it) 
     Intent cropIntent = new Intent("com.android.camera.action.CROP"); 
     // indicate image type and Uri 
     cropIntent.setDataAndType(picUri, "image/*"); 
     // set crop properties 
     cropIntent.putExtra("crop", "true"); 
     int asp = (int) (DeviceDimensions.getScreenWidth() - 80)/187; 
     // indicate aspect of desired crop 
     cropIntent.putExtra("aspectX", asp); 
     cropIntent.putExtra("aspectY", 3); 
     // indicate output X and Y 
     cropIntent.putExtra("outputX", DeviceDimensions.getScreenWidth() - 80); 
     cropIntent.putExtra("outputY", 187*3); 
     // retrieve data on return 
     cropIntent.putExtra("return-data", true); 
     // start the activity - we handle returning in onActivityResult 
     startActivityForResult(cropIntent, PIC_CROP); 
    } 
    // respond to users whose devices do not support the crop action 
    catch (ActivityNotFoundException anfe) { 
     Toast toast = Toast 
       .makeText(this, "This device doesn't support the crop action!", Toast.LENGTH_SHORT); 
     toast.show(); 
    } 
} 

而且OnActivity结果进行图像裁切是:

if (requestCode == PIC_CROP) { 
      // get the returned data 
      Bundle extras = data.getExtras(); 
      // get the cropped bitmap 
      Bitmap thePic = extras.getParcelable("data"); 
      parentimage.setImageBitmap(thePic); 
     } 
+2

请注意,裁剪功能对于Android不是强制性的,有些设备可能没有'com.android.camera.action.CROP'。所以使用外部裁剪功能是个不错的主意。我最好找一个作物的图书馆,并使用它。 –

+0

好的。 Thnx但为什么会发生这种情况,在相机的情况下? – Vinay

+0

[未发现可处理Intent com.android.camera.action.CROP的活动]的可能重复(https://stackoverflow.com/questions/41890891/no-activity-found-to-handle-intent-com-android- camera-action-crop) – W4R10CK

回答

0

这可能是命令后回来以不同的格式。 intents中的文件可以隐藏在几个不同的地方,可以通过几种不同的方式访问这些文件,从而使流引导。还有一些有权限的项目。相机意图可能会以一种方式为您提供数据,作物意图可能会以不同的方式返回裁剪后的数据。所以你不能指望两者是相同的。你必须覆盖所有的基础。

请记住,这只是您设备的CROP功能。其他设备没有裁剪功能,它们也可能工作不同。我不相信他们中的很多人。它实际上是绘制盒子并在另一个位图上绘制位图。我使用第三方库并将其包含在您的应用中。然后你可以确定它的工作。

虽然听起来不错。您无法确定该功能是否存在。更少的是它将以一致的方式工作。

如果我记得文件可以在文件流中的额外内容。它可以是content://或file://对象。而权限可能会变得三元。画廊倾向于将它们作为content://文件返回,而不命名后缀,而相机可能会给出可以读取的文件后缀。

我已经看过这个东西几次,你需要查找从图库返回的东西的名称知道文件类型,而其他时候,它包含的URI正确给你正确的后缀。

这些和其他原因基本上使作物意图功能没有价值。如果我不得不猜测文件存储方式与您从裁剪返回时所期望的方式不同。我使用类似:

public Uri getUriFromIntent(Intent intent) { 
    Uri uri = intent.getData(); 
    if (uri != null) return uri; 

    Bundle bundle = intent.getExtras(); 
    if (bundle == null) return null; 

    Object object = bundle.get(Intent.EXTRA_STREAM); 
    if (object instanceof Uri) { 
     return (Uri) object; 
    } 
    return null; 
} 

在寻找的希望在那里它可能是,因为不同的事情将文件放在一堆不同的地方,它简直疯了试图找到他们,如果你不知道确切的服务这给了你的URI。