2016-02-05 38 views
0

我的應用程序有一個ImageView活動,我從手機圖庫中挑選一張圖片,並將其設置在此ImageView中作爲用戶的個人資料圖片。將從圖庫中選取的圖像調整爲圖像視圖

我的問題是,一些圖片時,選擇使應用程序停止原因是太大,我想知道如果有人可以看我的代碼,並幫助我如何調整這張選擇的圖片,所以設置後,用戶可以在設置之前剪下這張圖片,下面是我的代碼,我在這張圖片上拍照。如果有人做了必要的改變,並且給我代碼,我會非常感激,因爲我對開發不瞭解太多。謝謝。

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) { 
     Uri selectedImage = data.getData(); 
     String[] filePathColumn = { MediaStore.Images.Media.DATA }; 

     Cursor cursor = getContentResolver().query(selectedImage, 
       filePathColumn, null, null, null); 
     cursor.moveToFirst(); 

     int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
     String picturePath = cursor.getString(columnIndex); 
     PreferenceManager.getDefaultSharedPreferences(this).edit().putString("picturePath", picturePath).commit(); 
     cursor.close(); 

     ImageView imageView = (ImageView) findViewById(R.id.User); 
     imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath)); 

    } 

    } 
+0

嘗試,這是圖像壓縮像whtsapp我已經使用在我的應用程序中https://www.built.io/blog/2013/03/improving-image-compression-what-weve-learned-from-whatsapp/ –

回答

0

不是要重寫代碼,但是這可能是從我的代碼用於縮放位圖向下有用

Bitmap bitmap = BitmapFactory.decodeFile(foto.getFotoOrderFilePath()); 
    Double height = (double)bitmap.getHeight(); 
    Double scalingFactor = (960.0/height); 
    int tempWidht = bitmap.getWidth(); 
    Double Dwidth = (tempWidht*scalingFactor); 
    int width = Dwidth.intValue(); 
    Log.v("bitmap dimensions: ", String.valueOf(height) + " + " +String.valueOf(width) + " + " + String.valueOf(scalingFactor)); 
    bitmap = Utilities.scaleBitmap(bitmap, width, 960); 

的摘錄。它將高度設置爲960,並通過縮放來相應地改變寬度。

編輯:

ScaleBitmap方法。

public static Bitmap scaleBitmap(Bitmap bitmap, int wantedWidth, int wantedHeight) { 
    Bitmap output = Bitmap.createBitmap(wantedWidth, wantedHeight, Config.ARGB_8888); 
    Canvas canvas = new Canvas(output); 
    Matrix m = new Matrix(); 
    m.setScale((float) wantedWidth/bitmap.getWidth(), (float) wantedHeight/bitmap.getHeight()); 
    canvas.drawBitmap(bitmap, m, new Paint()); 
    return output; 
} 

遺憾的響應晚

+0

我不太瞭解編碼,我應該在哪裏添加它我的代碼?再次感謝你:)我開始編碼。 –

+0

我加入了我的代碼,但是這一行「bitmap = Utilities.scaleBitmap(bitmap,width,960);」 Ultilities保持紅色,並且第一個位圖保持未使用狀態。 –

+0

嗯,忘了吧,如果你還需要的話,下週一會發布這個代碼 – Kezufru

0

您可以使用畢加索庫已經使用。從here得到它。 語法如下:

Picasso.with(getContext()).load(imagePath).into(imageView); 
0

你可以試試這個代碼來調整圖像按您的要求

public Bitmap decodeSampledBitmapFromResource(String Filepath, 
                int reqWidth, int reqHeight) { 

    // First decode with inJustDecodeBounds=true to check dimensions 
    final BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inJustDecodeBounds = true; 
    BitmapFactory.decodeFile(Filepath, options); 

    // Calculate inSampleSize 
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); 

    // Decode bitmap with inSampleSize set 
    options.inJustDecodeBounds = false; 
    return BitmapFactory.decodeFile(Filepath, options); 
} 

public int calculateInSampleSize(
     BitmapFactory.Options options, int reqWidth, int reqHeight) { 

    // Raw height and width of image 
    int inSampleSize = 1; 
    final int height = options.outHeight; 
    final int width = options.outWidth; 


    if (height > reqHeight || width > reqWidth) { 

     final int halfHeight = height/2; 
     final int halfWidth = width/2; 

     // Calculate the largest inSampleSize value that is a power of 2 and keeps both 
     // height and width larger than the requested height and width. 
     while ((halfHeight/inSampleSize) > reqHeight 
       && (halfWidth/inSampleSize) > reqWidth) { 
      inSampleSize *= 2; 
     } 
    } 

    return inSampleSize; 
} 

檢查這裏 http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

+0

但我必須在哪裏以及如何調用decodeSampledBitmapFromResource?謝謝。 –

+0

@JaquelineRosa一旦你得到圖像路徑傳遞給這個方法,你會得到它調整大小的位圖,然後將其設置爲imageview。我希望你能理解它。 – androidnoobdev

相關問題