2012-02-15 65 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中回答它的错误。所以请检查它或给你的代码通过它。

谢谢。