2015-07-13 56 views
0

我正在編寫一個代碼,使用戶可以從圖庫中選擇個人資料圖片。到目前爲止,代碼如下。 (1)在活動下的字段中,我聲明瞭一個按鈕,一個imageview和一個靜態整數。從圖庫中選擇一個圖片Android

private Button btnChooseProfile; 
private ImageView ivProfile; 
private static final int RESULT_LOAD_IMAGE = 1; 

(2)現在,我通過onCreate方法在屏幕上看到它們。

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(); 
    setContentView(R.layout.activity_register); 

    btnChooseProfile = (Button) findViewById(R.id.btnChooseProfile); 
    ivProfile = (ImageView) findViewById(R.id.ivProfile); 

    btnChooseProfile.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
      startActivityForResult(intent, RESULT_LOAD_IMAGE); 
     } 
}; 

(3)最後,我推翻了onActivityResult方法來定義一下btnChooseProfile按鈕後,會發生什麼。

@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 imagePath = cursor.getString(columnIndex); 
      cursor.close(); 

      ivProfile.setImageBitmap(BitmapFactory.decodeFile(imagePath)); 
     } 
    } 

當我運行代碼時,我設法訪問該庫。選擇圖像後,我會希望imageview包含一張選定的圖片,但不知何故應用程序會結束。

我已經閱讀了很多在網站上提出的解決方案,而且代碼片段本身似乎沒有問題。如果我錯過了一些東西,請給我一些建議,這將非常感激。

謝謝。

編輯這裏的logcat。 (日誌級別:調試)

07-13 15:15:25.961 28110-28110/com.marshall.gruppo E/﹕ Device driver API match 
    Device driver API version: 29 
    User space API version: 29 
07-13 15:15:25.961 28110-28110/com.marshall.gruppo E/﹕ mali: REVISION=Linux-r3p2-01rel3 BUILD_DATE=Tue Jul 22 19:59:34 KST 2014 
07-13 15:15:26.051 28110-28110/com.marshall.gruppo D/OpenGLRenderer﹕ Enabling debug mode 0 
07-13 15:15:28.726 28110-28110/com.marshall.gruppo D/dalvikvm﹕ GC_FOR_ALLOC freed 153K, 6% free 14269K/15064K, paused 9ms, total 9ms 
07-13 15:15:28.751 28110-28110/com.marshall.gruppo D/AbsListView﹕ Get MotionRecognitionManager 
07-13 15:15:28.831 28110-28110/com.marshall.gruppo D/dalvikvm﹕ GC_FOR_ALLOC freed 115K, 6% free 14329K/15088K, paused 12ms, total 12ms 
07-13 15:15:28.861 28110-28110/com.marshall.gruppo I/dalvikvm-heap﹕ Grow heap (frag case) to 33.649MB for 19955728-byte allocation 
07-13 15:15:28.876 28110-28119/com.marshall.gruppo D/dalvikvm﹕ GC_FOR_ALLOC freed 1K, 3% free 33816K/34580K, paused 13ms, total 13ms 
07-13 15:15:33.401 28110-28110/com.marshall.gruppo W/IInputConnectionWrapper﹕ showStatusIcon on inactive InputConnection 
07-13 15:15:37.681 28110-28110/com.marshall.gruppo D/AbsListView﹕ onDetachedFromWindow 
+0

的logcat登錄請。 –

+0

@AnandSingh編輯。但logcat似乎並沒有太多顯示。 – MarshallLee

回答

0
public Bitmap compressImage(String imageUri) { 

     String filePath = imageUri; 
     // String filePath = getRealPathFromURI(imageUri); 
     Bitmap scaledBitmap = null; 

     BitmapFactory.Options options = new BitmapFactory.Options(); 

     // by setting this field as true, the actual bitmap pixels are not 
     // loaded in the memory. Just the bounds are loaded. If 
     // you try the use the bitmap here, you will get null. 
     options.inJustDecodeBounds = true; 
     Bitmap bmp = BitmapFactory.decodeFile(filePath, options); 

     int actualHeight = options.outHeight; 
     int actualWidth = options.outWidth; 

     // max Height and width values of the compressed image is taken as 
     // 816x612 

     /* 
     * float maxHeight = 816.0f; float maxWidth = 612.0f; 
     */ 
     float maxHeight = 1080.0f; 
     float maxWidth = 800.0f; 

     float imgRatio = actualWidth/(float) actualHeight; 
     float maxRatio = maxWidth/(float) maxHeight; 

     // width and height values are set maintaining the aspect ratio of the 
     // image 

     if (actualHeight > maxHeight || actualWidth > maxWidth) { 
      if (imgRatio < maxRatio) { 
       imgRatio = maxHeight/actualHeight; 
       actualWidth = (int) (imgRatio * actualWidth); 
       actualHeight = (int) maxHeight; 
      } else if (imgRatio > maxRatio) { 
       imgRatio = maxWidth/actualWidth; 
       actualHeight = (int) (imgRatio * actualHeight); 
       actualWidth = (int) maxWidth; 
      } else { 
       actualHeight = (int) maxHeight; 
       actualWidth = (int) maxWidth; 

      } 
     } 

     // setting inSampleSize value allows to load a scaled down version of 
     // the original image 

     options.inSampleSize = calculateInSampleSize(options, actualWidth, 
       actualHeight); 

     // inJustDecodeBounds set to false to load the actual bitmap 
     options.inJustDecodeBounds = false; 

     // this options allow android to claim the bitmap memory if it runs low 
     // on memory 
     options.inPurgeable = true; 
     options.inInputShareable = true; 
     options.inTempStorage = new byte[16 * 1024]; 

     try { 
      // load the bitmap from its path 
      bmp = BitmapFactory.decodeFile(filePath, options); 
     } catch (OutOfMemoryError exception) { 
      exception.printStackTrace(); 

     } 
     try { 
      scaledBitmap = Bitmap.createBitmap(actualWidth, actualHeight, 
        Bitmap.Config.ARGB_8888); 
     } catch (OutOfMemoryError exception) { 
      exception.printStackTrace(); 
     } 

     float ratioX = actualWidth/(float) options.outWidth; 
     float ratioY = actualHeight/(float) options.outHeight; 
     float middleX = actualWidth/2.0f; 
     float middleY = actualHeight/2.0f; 

     Matrix scaleMatrix = new Matrix(); 
     scaleMatrix.setScale(ratioX, ratioY, middleX, middleY); 

     Canvas canvas = new Canvas(scaledBitmap); 
     canvas.setMatrix(scaleMatrix); 
     canvas.drawBitmap(bmp, middleX - bmp.getWidth()/2, 
       middleY - bmp.getHeight()/2, new Paint(
         Paint.FILTER_BITMAP_FLAG)); 

     // check the rotation of the image and display it properly 
     ExifInterface exif; 
     try { 
      exif = new ExifInterface(filePath); 

      int orientation = exif.getAttributeInt(
        ExifInterface.TAG_ORIENTATION, 0); 
      Log.d("EXIF", "Exif: " + orientation); 
      Matrix matrix = new Matrix(); 
      if (orientation == 6) { 
       matrix.postRotate(90); 
       Log.d("EXIF", "Exif: " + orientation); 
      } else if (orientation == 3) { 
       matrix.postRotate(180); 
       Log.d("EXIF", "Exif: " + orientation); 
      } else if (orientation == 8) { 
       matrix.postRotate(270); 
       Log.d("EXIF", "Exif: " + orientation); 
      } 

      scaledBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, 
        scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, 
        true); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return scaledBitmap; 
     } 
+0

因此,基本上我的代碼由於尺寸問題不起作用,呃? – MarshallLee

+0

是的,因爲位圖內存不足崩潰 –

0

我不會做所有這些你在onActivityResult做的事情......這是主線程,所以訪問DB(ContentProvider的)和解碼位圖不推薦。啓動一個AsyncTask或其他東西。

有時返回的數據是空的(是的,它發生) - 讓你的代碼無效的安全。添加一些日誌。

我相信你應該很好。

0

它可能是一個大的圖片尺寸問題。試試這段代碼來減少圖片的大小。

Bitmap photo; 
     File file = new File(picturePath); 
     int file_size = Integer 
       .parseInt(String.valueOf(file.length()/1024)); 
     if (file_size > 2048) 
     { 
      BitmapFactory.Options options = new BitmapFactory.Options(); 
      options.inSampleSize = 4; 
      photo = BitmapFactory.decodeFile(picturePath, options); 
     } else if (file_size > 1024) 
     { 
      BitmapFactory.Options options = new BitmapFactory.Options(); 
      options.inSampleSize = 2; 
      photo = BitmapFactory.decodeFile(picturePath, options); 
     } else 
      photo = BitmapFactory.decodeFile(picturePath); 
     ivProfile.setImageBitmap(photo);