簡體   English   中英

在Android中裁剪圖像

[英]Cropping image in android

每次使用camera裁剪圖像時,都會出現Unable to load image的錯誤。 但就gallery ,它運行良好。

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);
        }

命令后,它可能以其他格式返回。 意向文件可以隱藏在幾個不同的位置,可以用幾種不同的方式訪問它們以啟動流。 還有一些具有權限的項目。 攝影機意圖可能會以一種方式為您提供數據,裁切意圖可能會以另一種方式返回裁切后的數據。 因此,您不能期望兩者相同。 您必須涵蓋所有基礎。

請記住,這只是設備的CROP功能。 其他設備沒有裁剪功能,它們的工作方式也可能不同。 我不相信他們很多。 它實際上是在繪制框並在另一個位圖中繪制一個位圖。 我使用第3方庫,並將其包含在您的應用中。 然后,您可以確定它是否有效。

雖然聽起來不錯。 您不能確定該功能是否存在。 更不用說它將以一致的方式工作。

如果我記得該文件可以放在文件流中的其他文件中。 它可以是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的確切服務,嘗試查找它們確實很麻煩。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM