以前我使用以下方法将Bitmap
图片转换为String
public String getStringImage(Bitmap bmp) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
return encodedImage;
}
但是现在我有一个File
字节形式,我希望得到这个文件的String
我试过了
public String getStringbytes(byte[] buffer) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
buffer.compress(Bitmap.CompressFormat.JPEG, 100, baos);// this is the error
byte[] imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
return encodedImage;
}
如何将传递给方法的字节转换为String
?换句话说,我如何修改我在压缩Bitmap
图像之前捕获要转换为String
的字节的部分?
答案 0 :(得分:1)
我不确定你问的是什么,但你问的是这个:
“我有一个函数将Bitmap作为输入并通过执行以下转换步骤返回String:Bitmap - > byte [] - > base64 String,
但我还想要一个以Byte数组作为输入的函数,并返回一个带有以下转换步骤的String:byte [] - > base64字符串“
然后试试这个:
public String getStringbytes(byte[] buffer) {
String encodedImage = Base64.encodeToString(buffer, Base64.DEFAULT);
return encodedImage;
}
答案 1 :(得分:0)
通过BitmapFactory.decodeByteArray将178 error Windows_NT 10.0.10586
179 error argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "install"
180 error node v4.4.7
181 error npm v2.15.8
182 error code EPEERINVALID
183 error peerinvalid The package @angular/common@2.0.0-rc.7 does not satisfy its siblings' peerDependencies requirements!
183 error peerinvalid Peer @angular/forms@2.0.0-rc.7 wants @angular/common@^2.0.0-rc.7
183 error peerinvalid Peer @angular/platform-browser@2.0.0-rc.7 wants @angular/common@^2.0.0-rc.7
183 error peerinvalid Peer @angular/platform-browser-dynamic@2.0.0-rc.7 wants @angular/common@^2.0.0-rc.7
183 error peerinvalid Peer @angular/router@3.0.0-rc.3 wants @angular/common@^2.0.0-rc.7
183 error peerinvalid Peer @angular/platform-server@2.0.0 wants @angular/common@^2.0.0v
184 verbose exit [ 1, true ]
转换为位图并将新的位图传递给第一种方法会不会更容易?