2012-02-15 62 views
1

我正在嘗試爲我的相對佈局動態添加縮略圖。這是代碼在這裏在Android中動態添加縮略圖

public void showViewOfReceipt(String fileName) 
     { 
      byte[] imageData = null; 

      try 
      { 

      final int THUMBNAIL_SIZE = 64; 

      FileInputStream fis = new FileInputStream(fileName); 
      Bitmap imageBitmap = BitmapFactory.decodeStream(fis); 

      Float width = new Float(imageBitmap.getWidth()); 
      Float height = new Float(imageBitmap.getHeight()); 
      Float ratio = width/height; 
      imageBitmap = Bitmap.createScaledBitmap(imageBitmap, (int)(THUMBNAIL_SIZE * ratio), THUMBNAIL_SIZE, false); 

      ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
      imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
      imageData = baos.toByteArray(); 
      ImageView image = new ImageView(this); 

      image.setImageBitmap(imageBitmap); 
      RelativeLayout layout = (RelativeLayout) findViewById(R.id.expLayout5); 
      layout.addView(image); 
      } 
      catch(Exception ex) { 
      } 
     } 

它從來沒有顯示任何

問候

+0

你最好看看有沒有在您的代碼中拋出異常。它會告訴你更多關於創建'Bitmap'或'FileInputStream'等錯誤信息。 – 2012-02-15 05:32:30

+0

寫入EMPTY Catch塊是不好的做法,總是把至少一個日誌放在那裏。 – MKJParekh 2012-02-15 05:36:51

+0

你可以把整個代碼放在這裏嗎? – Deva 2012-02-15 06:22:12

回答

1

1-更改您的相對佈局,以線性佈局 2-使用下面這段代碼來獲取位圖圖像

Uri photoUri = data.getData(); 
        String[] filePathColumn = {MediaStore.Images.Media.DATA}; 
        Cursor cursor = getContentResolver().query(photoUri, filePathColumn, null, null, null); 
         if (cursor.moveToFirst()) 
         { 
          int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
          String filePath = cursor.getString(columnIndex); 
          cursor.close(); 
          Bitmap imageReturned = BitmapFactory.decodeFile(filePath); 

          showImageInLayout(imageReturned); 

然後定義一個函數showImageInLayout(Bitmap imageReturned)

public void showViewOfReceiptInLayout(Bitmap imageBitmap) 
     { 
      byte[] imageData = null; 
       imageBitmap = Bitmap.createScaledBitmap(imageBitmap, yourWidth, yourHeight, false); 
       ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
       imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
       imageData = baos.toByteArray(); 
       ImageView image = new ImageView(this); 
       image.setImageBitmap(imageBitmap); 
       layout.addView(image); 

     } 
1

更改下面的代碼行,

catch(Exception ex) 
{ 
} 

到,

catch(Exception ex) 
{ 
     e.printStack(); 
} 

所以,你會得到錯誤,如果有的話。

+0

沒有檢測到錯誤 – 2012-02-15 05:44:34

+0

抱歉它說沒有找到文件。內容:// media/external/images/media/1是仿真器發送給我的文件的路徑 – 2012-02-15 06:20:01

+0

這意味着您的文件路徑不正確,只需更正即可。 – Altaaf 2012-02-15 06:21:28

0

評論代碼行:

//圖像=(ImageView的)findViewById(R.id.imageView1);

+0

已將其刪除,無效。對不起,這是一個錯誤 – 2012-02-15 05:43:56

0

請更新錯誤或異常日誌代碼的問題,以便我們能夠幫助您。

並根據Altaaf在fileName中回答它的錯誤。所以請檢查它或給你的代碼通過它。

謝謝。