2015-11-03 33 views
0

現在我開發了一個需要將圖像上傳到我的服務器的android應用程序。但是當我嘗試加載圖像到ImageView它只是顯示空白。當我將分辨率從1300像素更改爲300像素時,圖像可顯示在ImageView。我的代碼我喜歡下面。請幫忙如何調整從相機捕獲的圖像並在imageview上顯示

protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    { 
     super.onActivityResult(requestCode, resultCode, data); 
     if (resultCode == RESULT_OK) 
     { 
        viewImage = (ImageView)findViewById(R.id.imgInv); 
        int width = viewImage.getWidth(); 
        int height = viewImage.getHeight(); 

        Bitmap bitmap; 
        BitmapFactory.Options bitmapOptions = new BitmapFactory.Options(); 

        bitmapOptions.inJustDecodeBounds = true; 

        bitmapOptions.inJustDecodeBounds = false; 

        bitmapOptions.inPreferredConfig = Bitmap.Config.RGB_565; 
        bitmapOptions.inDither = true; 
        bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(),bitmapOptions); 

        viewImage.setImageBitmap(bitmap); 

        OutputStream outFile = null; 
        File file = new File(path,String.valueOf(System.currentTimeMillis()) + ".jpg"); 
        imgName = mName + "_" + String.valueOf(System.currentTimeMillis()); 

        ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
        bitmap.compress(Bitmap.CompressFormat.JPEG, 75, baos); 
        byte[] b = baos.toByteArray(); 
        imgData = Base64.encodeToString(b,Base64.DEFAULT); 
     } 

} 
+0

你得到一個有效的「位圖」嗎?你可以只添加日誌並查看'bitmap.getHeight()'和'bitmap.getWidth()'嗎? – Henry

+0

爲什麼你這樣做:bitmapOptions.inJustDecodeBounds = true; bitmapOptions.inJustDecodeBounds = false; –

+0

我遵循我在網上找到的代碼。 – vic3ai

回答

0

我不是在談論你的代碼,但有一個你想要的庫。
您可以使用Picasso函數庫,該函數庫具有用於處理下載,緩存等圖像的完整功能。 您可以通過一個示例從here獲取它。
另外還有一個關於它的tutorial

相關問題