2016-02-05 60 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