2013-01-04 43 views
-3

我想将URL转换为位图image.i我也试图在SD卡上压缩和保存此位图,但是当我运行代码时,Web Image-view中不显示任何图像。我有以下代码:如何将位图转换为字节阵列

Logger.d(LOG_TAG, "Enter retrieveImageData()"); 
    URL url = new URL(imageUrl); 
    HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
    connection.setReadTimeout(CONNECTTION_TIMEOUT); 
    // determine the image size and allocate a buffer 
    int fileSize = connection.getContentLength(); 
    if (fileSize < 0) { 
     Logger.d(LOG_TAG, "retrieveImageData()->file size less than 0"); 
     return null; 
    } 
    byte[] imageData = null; 

    byte[] buffer = new byte[BUFFER_SIZE]; 

    // download the file 
    // if(Global.show_logs) Log.d(LOG_TAG, "fetching image " + imageUrl + 
    // " (" + fileSize + ")"); 
    BufferedInputStream istream = new BufferedInputStream(connection.getInputStream()); 

    if ((!(height == -1)) && (!(width == -1))) { 
     Logger.d(LOG_TAG, "Enter retrieveImageData() :width="+width+" height"+height); 
     File tmpFile = GlobalFunctions.getTmpFile(); 

     if (tmpFile == null) 
      throw new IOException("DroidFu::ImageLoader: Could not create temp file!"); 

     BufferedOutputStream ostream = new BufferedOutputStream(new FileOutputStream(tmpFile)); 

     Logger.d(LOG_TAG, "before call of IOUtils.copy"); 
     IOUtils.copy(istream, ostream); 
     Logger.d(LOG_TAG, "after call of IOUtils.copy"); 
     // First decode with inJustDecodeBounds=true to check dimensions 
     final BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inJustDecodeBounds = true; 
     BitmapFactory.decodeFile(tmpFile.getAbsolutePath(), options); 

     // Calculate inSampleSize 
     Logger.d(LOG_TAG, "before call of calculateInSampleSize()"); 
     options.inSampleSize = calculateInSampleSize(options, width, height); 
     Logger.d(LOG_TAG, "after call of calculateInSampleSize()"); 

     // Decode bitmap with inSampleSize set 
     options.inJustDecodeBounds = false; 
     Logger.d(LOG_TAG, "Absolute path of tmp file is: "+tmpFile.getAbsolutePath()); 
     Bitmap b=BitmapFactory.decodeFile(tmpFile.getAbsolutePath(), options); 
     ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
     b.compress(Bitmap.CompressFormat.PNG, 100, stream); 

     byte[] byteArray = stream.toByteArray(); 

     b.recycle(); 
     //imageData = stream.toByteArray(); 

     istream.close(); 
     ostream.close(); 
     stream.close(); 
     Logger.d(LOG_TAG, "Exit retrieveImageData() after resizing to imageview"); 
     return byteArray; 
    } 

但在这行的代码抛出异常(的Throwable e)和也的 “e” 为null:

b.compress(Bitmap.CompressFormat.PNG, 100, stream); 
byte[] byteArray = stream.toByteArray(); 
+8

-1表示没有张贴堆栈跟踪 – njzk2

+0

堆栈跟踪是没有帮助的(没有信息),这就是为什么,我没有分享。任何如何,如果你能帮助我,我会分享。 –

+0

@MohammadImran我们应该调试屏幕上的代码吗? :-)你应该分享你的堆栈跟踪。 – stoilkov

回答

4

这里是链接更先进的image Loader library

的用法如下:

第一将此代码放在您的主要活动。

DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder().showStubImage(R.drawable.ic_stub).showImageOnFail(R.drawable.ic_error).showImageForEmptyUri(R.drawable.ic_empty_url).cacheInMemory().cacheOnDisc().build(); 
    // Create global configuration and initialize ImageLoader with this 
    // configuration 
    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext()).defaultDisplayImageOptions(defaultOptions).build(); 
    ImageLoader.getInstance().init(config); 

然后用它在你的类是这样的:

private ImageLoader imageLoader; 

您的onCreate()方法

imageLoader = ImageLoader.getInstance()内;

然后加载图像这样

imageLoader.displayImage(IMG_URL, imageView); 
7

试试这个。

转换位图到字节数组: -

Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); 
ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream); 
byte[] byteArray = stream.toByteArray(); 

转换字节数组转换为位图图像: -

Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length); 
+1

你和上面的代码有什么区别? –

+2

@AliImran看看日期。这是第一个答案。 – Phil

3

以位图转换成字节数组,你可以使用

 final int lnth=bitmap.getByteCount(); 

     ByteBuffer dst= ByteBuffer.allocate(lnth); 
     bitmap.copyPixelsToBuffer(dst); 
     byte[] barray=dst.array(); 

而且从字节数组中获取位图使用

Bitmap bitmap = new BitmapFactory().decodeByteArray(byte_array, 0/* starting index*/, byte_array.length/*no of byte to read*/)