2012-05-27 7 views
10

मैं एक एंड्रॉइड बना रहा हूं जहां। एक गतिविधि के अंदर मेरे पास एक छवि बटन है। जब मैं उस पर क्लिक करता हूं तो गैलरी खुलती है और मैं एक छवि चुन सकता हूं। फिर मैंने उस छवि को छवि बटन के लिए नई छवि के रूप में सेट किया। समस्या यह है कि छवि मेरी गतिविधि के अंदर बहुत बड़ी दिखाई देती है। मैं इसे अपने छवि बटन में कैसे फिट कर सकता हूं?एंड्रॉइड में गैलरी से चुनी गई छवि का आकार कैसे बदलें?

protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

    switch(requestCode) { 
    case SELECT_PHOTO: 
     if(resultCode == RESULT_OK){ 
      Uri selectedImage = imageReturnedIntent.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 filePath = cursor.getString(columnIndex); 
      cursor.close(); 


      Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath); 

      mImageButton.setImageBitmap(yourSelectedImage); 
     } 
    } 
} 

उत्तर

33

आप का उपयोग एक आकार का आकार प्राप्त करने के लिए इस विधि का उपयोग कर सकते हैं। इस तरह से आप OutOfMemoryError से बच सकते हैं

public static Bitmap decodeUri(Context c, Uri uri, final int requiredSize) 
      throws FileNotFoundException { 
     BitmapFactory.Options o = new BitmapFactory.Options(); 
     o.inJustDecodeBounds = true; 
     BitmapFactory.decodeStream(c.getContentResolver().openInputStream(uri), null, o); 

     int width_tmp = o.outWidth 
       , height_tmp = o.outHeight; 
     int scale = 1; 

     while(true) { 
      if(width_tmp/2 < requiredSize || height_tmp/2 < requiredSize) 
       break; 
      width_tmp /= 2; 
      height_tmp /= 2; 
      scale *= 2; 
     } 

     BitmapFactory.Options o2 = new BitmapFactory.Options(); 
     o2.inSampleSize = scale; 
     return BitmapFactory.decodeStream(c.getContentResolver().openInputStream(uri), null, o2); 
    } 
+2

क्या आप समझा सकते हैं कि 'आवश्यक आकार' पैरामीटर का क्या अर्थ है? – TrungDQ

3

देखें इस LINK

उपयोग: Bitmap.createScaledBitmap (बिटमैप src, पूर्णांक dstWidth, पूर्णांक dstHeight, बूलियन फिल्टर)

या इन विधि ::

public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) { 
    int width = bm.getWidth(); 
    int height = bm.getHeight(); 
    float scaleWidth = ((float) newWidth)/width; 
    float scaleHeight = ((float) newHeight)/height; 

    // create a matrix for the manipulation 
    Matrix matrix = new Matrix(); 

    // resize the bit map 
    matrix.postScale(scaleWidth, scaleHeight); 

    // recreate the new Bitmap 
    Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false); 

    return resizedBitmap; 
} 
+0

मैं फोन करेगा कि विधि के बाद: बिटमैप yourSelectedImage = BitmapFactory.decodeFile (filepath); – user1420042

+0

हाँ आप –

+0

पर कॉल कर सकते हैं लेकिन फिर मुझे रनटाइम त्रुटि मिलती है – user1420042

संबंधित मुद्दे