Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/210.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 位图到基64编码字符串_Android_Image_Encoding_Bitmap - Fatal编程技术网

Android 位图到基64编码字符串

Android 位图到基64编码字符串,android,image,encoding,bitmap,Android,Image,Encoding,Bitmap,我正在尝试将图像转换为位图,然后将其编码为base64字符串。我在这一行遇到空指针异常 bMap.compress(Bitmap.CompressFormat.PNG, COMPRESSION_QUALITY, byteArrayBitmapStream); 怎么了?请注意,我正在向下面的方法输入带有图片“pic2.jpg”的文件名 下面是我的代码: private String convertToBitmap (String name){ File

我正在尝试将图像转换为位图,然后将其编码为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
试试这个

    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;
如果您需要进一步优化图像

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;
    }

你应该试试这个:

 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;
        }
}

我得到一个空指针异常
你的意思是
bMap
为空吗?我相信是的,我认为我使用的文件路径不正确。但是我已经从终端获取了文件路径,并将其放入方法中,但仍然不起作用。可能文件太大了。检查此答案:我已将日志猫添加到post@Geek96是,请确保路径正确,如果指定的文件名为null,或者无法解码为位图,则函数返回null,请检查。使用
try catch
并打印日志
 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;
        }
}