我正在尝试将图像转换为位图,然后将其编码为base64字符串。我在行上得到空指针异常
bMap.compress(Bitmap.CompressFormat.PNG, COMPRESSION_QUALITY,
byteArrayBitmapStream);
有什么不对?请注意,我正在使用图片“pic2.jpg”输入文件名到下面的方法。
下面是我的代码:
private String convertToBitmap (String name){
File imgFile = new File (name);
Bitmap bMap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
final int COMPRESSION_QUALITY = 100;
String encodedImage;
ByteArrayOutputStream byteArrayBitmapStream = new ByteArrayOutputStream();
bMap.compress(Bitmap.CompressFormat.PNG, COMPRESSION_QUALITY,
byteArrayBitmapStream);
byte[] b = byteArrayBitmapStream.toByteArray();
encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
return encodedImage;
}
在我的日志猫下面:
03-22 23:03:44.916 18331-18331/? I/art: Not late-enabling -Xcheck:jni (already on)
03-22 23:03:44.916 18331-18331/? W/art: Unexpected CPU variant for X86 using defaults: x86
03-22 23:03:45.029 18331-18331/com.example.reynaldo.getimageserver W/System: ClassLoader referenced unknown path: /data/app/com.example.reynaldo.getimageserver-2/lib/x86
03-22 23:03:45.038 18331-18331/com.example.reynaldo.getimageserver I/InstantRun: Instant Run Runtime started. Android package is com.example.reynaldo.getimageserver, real application class is null.
03-22 23:03:45.124 18331-18331/com.example.reynaldo.getimageserver W/System: ClassLoader referenced unknown path: /data/app/com.example.reynaldo.getimageserver-2/lib/x86
03-22 23:03:45.372 18331-18331/com.example.reynaldo.getimageserver W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
03-22 23:03:45.676 18331-18361/com.example.reynaldo.getimageserver D/NetworkSecurityConfig: No Network Security Config specified, using platform default
03-22 23:03:45.865 18331-18367/com.example.reynaldo.getimageserver I/OpenGLRenderer: Initialized EGL, version 1.4
03-22 23:03:45.865 18331-18367/com.example.reynaldo.getimageserver D/OpenGLRenderer: Swap behavior 1
03-22 23:03:45.908 18331-18367/com.example.reynaldo.getimageserver E/EGL_emulation: tid 18367: eglSurfaceAttrib(1174): error 0x3009 (EGL_BAD_MATCH)
03-22 23:03:45.909 18331-18367/com.example.reynaldo.getimageserver W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xb493fe00, error=EGL_BAD_MATCH
答案 0 :(得分:0)
试试这个
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image); // your pic2
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] bitmapdata = stream.toByteArray();
encodedImage = Base64.encodeToString(bitmapdata, Base64.DEFAULT);
return encodedImage;
答案 1 :(得分:0)
Bitmap bitmap = optimizePhoto();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
Strng encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
如果您需要优化图像更新
private Bitmap optimizePhoto() {
// Get the dimensions of the View
int targetW = 300;
int targetH = 300;
// Get the dimensions of the bitmap
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(getArguments().getString("imagePath"), bmOptions);
int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;
// Determine how much to scale down the image
int scaleFactor = Math.min(photoW / targetW, photoH / targetH);
// Decode the image file into a Bitmap sized to fill the View
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;
bmOptions.inPurgeable = true;
Bitmap bitmap = BitmapFactory.decodeFile(getArguments().getString("imagePath"), bmOptions);
return bitmap;
}
答案 2 :(得分:0)
你应该试试这个:
private String convertToBitmap (String name){
File imgFile = new File (name);
Bitmap bMap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
if (bMap != null) {
bMap = Bitmap.createScaledBitmap(bMap, 256, 256, true);
String encodedImage;
ByteArrayOutputStream byteArrayBitmapStream = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayBitmapStream);
byte[] b = byteArrayBitmapStream.toByteArray();
encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
StringBuilder sb = new StringBuilder();
sb.append(encodedImage);
encodeString = sb.toString();
return encodedImage;
}
}