android中bitmap与字节流之间的相互转换

  1. 将字节数组转换为ImageView可调用的Bitmap对象,图片缩放,把字节数组保存为一个文件,把Bitmap转Byte  
  2. import java.io.BufferedOutputStream;    
  3. import java.io.ByteArrayOutputStream;    
  4. import java.io.File;    
  5. import java.io.FileOutputStream;    
  6. import java.io.IOException;    
  7. import java.io.InputStream;    
  8.     
  9. import android.graphics.Bitmap;    
  10. import android.graphics.BitmapFactory;    
  11. import android.graphics.Matrix;    
  12.     
  13. public class ImageDispose {    
  14.         
  15.         
  16.         
  17.     /**  
  18.      * @param 将图片内容解析成字节数组  
  19.      * @param inStream  
  20.      * @return byte[]  
  21.      * @throws Exception  
  22.      */    
  23.     public static byte[] readStream(InputStream inStream) throws Exception {    
  24.         byte[] buffer = new byte[1024];    
  25.         int len = -1;    
  26.         ByteArrayOutputStream outStream = new ByteArrayOutputStream();    
  27.         while ((len = inStream.read(buffer)) != -1) {    
  28.             outStream.write(buffer, 0, len);    
  29.         }    
  30.         byte[] data = outStream.toByteArray();    
  31.         outStream.close();    
  32.         inStream.close();    
  33.         return data;    
  34.     
  35.     }    
  36.     /**  
  37.      * @param 将字节数组转换为ImageView可调用的Bitmap对象  
  38.      * @param bytes  
  39.      * @param opts  
  40.      * @return Bitmap  
  41.      */    
  42.     public static Bitmap getPicFromBytes(byte[] bytes,    
  43.             BitmapFactory.Options opts) {    
  44.         if (bytes != null)    
  45.             if (opts != null)    
  46.                 return BitmapFactory.decodeByteArray(bytes, 0, bytes.length,    
  47.                         opts);    
  48.             else    
  49.                 return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);    
  50.         return null;    
  51.     }    
  52.     /**  
  53.      * @param 图片缩放  
  54.      * @param bitmap 对象  
  55.      * @param w 要缩放的宽度  
  56.      * @param h 要缩放的高度  
  57.      * @return newBmp 新 Bitmap对象  
  58.      */    
  59.     public static Bitmap zoomBitmap(Bitmap bitmap, int w, int h){    
  60.         int width = bitmap.getWidth();    
  61.         int height = bitmap.getHeight();    
  62.         Matrix matrix = new Matrix();    
  63.         float scaleWidth = ((float) w / width);    
  64.         float scaleHeight = ((float) h / height);    
  65.         matrix.postScale(scaleWidth, scaleHeight);    
  66.         Bitmap newBmp = Bitmap.createBitmap(bitmap, 00, width, height,    
  67.                 matrix, true);    
  68.         return newBmp;    
  69.     }    
  70.         
  71.     /**  
  72.      * 把Bitmap转Byte  
  73.      */    
  74.     public static byte[] Bitmap2Bytes(Bitmap bm){    
  75.         ByteArrayOutputStream baos = new ByteArrayOutputStream();    
  76.         bm.compress(Bitmap.CompressFormat.PNG, 100, baos);    
  77.         return baos.toByteArray();    
  78.     }    
  79.     /**  
  80.      * 把字节数组保存为一个文件  
  81.      */    
  82.     public static File getFileFromBytes(byte[] b, String outputFile) {    
  83.         BufferedOutputStream stream = null;    
  84.         File file = null;    
  85.         try {    
  86.             file = new File(outputFile);    
  87.             FileOutputStream fstream = new FileOutputStream(file);    
  88.             stream = new BufferedOutputStream(fstream);    
  89.             stream.write(b);    
  90.         } catch (Exception e) {    
  91.             e.printStackTrace();    
  92.         } finally {    
  93.             if (stream != null) {    
  94.                 try {    
  95.                     stream.close();    
  96.                 } catch (IOException e1) {    
  97.                     e1.printStackTrace();    
  98.                 }    
  99.             }    
  100.         }    
  101.         return file;    
  102.     }    
  103.             
  104. }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以使用ContentResolver的openInputStream方法将Uri转换字节流,示例代码如下: ```java public static byte[] uriToBytes(Context context, Uri uri) throws IOException { InputStream inputStream = context.getContentResolver().openInputStream(uri); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len; while ((len = inputStream.read(buffer)) != -1) { byteArrayOutputStream.write(buffer, , len); } return byteArrayOutputStream.toByteArray(); } ``` 其,context为上下文对象,uri为需要转换的Uri对象。 ### 回答2: 在Android开发,将Uri转换字节流的方式有几种。 一种方式是使用ContentResolver类的openInputStream()方法。可以通过getContentResolver()方法获取ContentResolver的实例对象。然后,调用openInputStream()方法,传入Uri参数来获取输入流。最后,可以通过输入流读取数据并将其转换字节数组。 另一种方式是使用File类的inputStream()方法。可以通过Uri获取File对象,然后调用File的inputStream()方法获取输入流。最后,同样可以通过输入流读取数据并转换字节数组。 例如,以下是使用ContentResolver类的openInputStream()方法将Uri转换字节流的示例代码: Uri uri = Uri.parse("content://com.example.provider/test"); ContentResolver contentResolver = getContentResolver(); InputStream inputStream = contentResolver.openInputStream(uri); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = inputStream.read(buffer)) != -1) { byteArrayOutputStream.write(buffer, 0, bytesRead); } byte[] byteArray = byteArrayOutputStream.toByteArray(); inputStream.close(); byteArrayOutputStream.close(); 通过以上代码,我们可以将Uri转换字节数组,然后可以根据需求进行相关处理,例如将字节数组保存到本地文件或者进行网络上传等操作。 ### 回答3: 在Android开发,将Uri转换字节流的方式有多种方法。以下是其一种常用的方式: 首先,我们需要从Uri获取输入流。可以通过ContentResolver类的openInputStream()方法来获得: ```java InputStream inputStream = getContentResolver().openInputStream(uri); ``` 接下来,我们可以使用ByteArrayOutputStream类将输入流转换字节流ByteArrayOutputStream类可以在内存创建一个字节数组缓冲区,并提供了一些方法来简化字节流的操作: ```java ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len; while ((len = inputStream.read(buffer)) != -1) { byteArrayOutputStream.write(buffer, 0, len); } ``` 最后,我们可以使用toByteArray()方法将字节流转换字节数组: ```java byte[] byteArray = byteArrayOutputStream.toByteArray(); ``` 现在,我们已经成功将Uri转换字节流。我们可以将字节流用于后续的操作,如保存到文件或上传到服务器等。 需要注意的是,在这个过程可能会出现异常,如FileNotFoundException和IOException。为了确保代码的健壮性,应该在合适的地方进行异常处理。 除了上述方法,还有其他一些方式可以实现相同的效果,比如使用FileProvider或通过Bitmap压缩等方式。选择哪种方式取决于具体的需求和场景。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值