Android 圖像系列: 將本地圖片加載到Drawable
- /**
- * 將文件生成位圖
- * @param path
- * @return
- * @throws IOException
- */
- public BitmapDrawable getImageDrawable(String path)
- throws IOException
- {
- //打開文件
- File file = new File(path);
- if(!file.exists())
- {
- return null;
- }
-
- ByteArrayOutputStream outStream = new ByteArrayOutputStream();
- byte[] bt = new byte[BUFFER_SIZE];
-
- //得到文件的輸入流
- InputStream in = new FileInputStream(file);
-
- //將文件讀出到輸出流中
- int readLength = in.read(bt);
- while (readLength != -1) {
- outStream.write(bt, 0, readLength);
- readLength = in.read(bt);
- }
-
- //轉換成byte 後 再格式化成位圖
- byte[] data = outStream.toByteArray();
- Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);// 生成位圖
- BitmapDrawable bd = new BitmapDrawable(bitmap);
-
- return bd;
- }