簡體   English   中英

將位圖保存到Android存儲中而不會降低質量

[英]Save bitmap to Android storage without losing quality

現在,我有一個Intent,可以打開手機的相機應用程序,允許用戶拍照,然后使用新圖像返回我的應用程序。 這樣,它返回一個位圖。 為了獲取圖片的Uri,以便可以將ImageView設置為它,我相信必須先將其保存到存儲中。 唯一的問題是當我的應用程序打開它時,圖像質量很差。 在需要壓縮的部分,我將質量保持在100,所以我不確定自己做錯了什么。

這是我啟動相機意圖的方式:

Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
    startActivityForResult(takePictureIntent, TAKE_PICTURE_INTENT_CODE);
}

這是我的處理方式:

//get result of image choosing
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    super.onActivityResult(requestCode, resultCode, data);

    switch(requestCode) {

        case TAKE_PICTURE_INTENT_CODE:
            if(resultCode == RESULT_OK){
                Bundle extras = data.getExtras();
                Bitmap imageBitmap = (Bitmap) extras.get("data");
                try {
                    switchToCropFrag(createImageFileFromCamera(imageBitmap));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }else if (resultCode != RESULT_CANCELED){
                Toast.makeText(this, "Failed to get image, please try again.", Toast.LENGTH_LONG).show();
            } else {    //user cancelled image picking
        }
    }
}

private Uri createImageFileFromCamera(Bitmap imageBitmap) throws IOException {
        ContextWrapper cw = new ContextWrapper(getApplicationContext());
        File directory = cw.getDir("temp", Context.MODE_PRIVATE);
        // Create imageDir
        File path = new File(directory, "temp.png");

        FileOutputStream fos = null;
        try {

            fos = new FileOutputStream(path);

            // Use the compress method on the BitMap object to write image to the OutputStream
            imageBitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return Uri.fromFile(path);
    }

對於switchToCropFrag,它僅使用Picasso設置圖像。 當我讓用戶從他們的圖庫中已經存在的手機中選擇照片時,圖像質量很好。

這樣,它返回一個位圖

它返回一個縮略圖。 引用文檔

如果不存在EXTRA_OUTPUT,則在額外字段中將一個小的圖像作為位圖對象返回

您似乎以為它返回的是常規尺寸的圖像,而事實並非如此。

為了獲取圖片的Uri,以便可以將ImageView設置為它,我相信必須先將其保存到存儲中。

如果這是您的活動中的ImageView ,則不需要Uri 調用setImageBitmap()

暫無
暫無

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

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