Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.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
Java 如何在android中将图像转换为base64字符串?_Java_Android_Android Studio - Fatal编程技术网

Java 如何在android中将图像转换为base64字符串?

Java 如何在android中将图像转换为base64字符串?,java,android,android-studio,Java,Android,Android Studio,这是我的密码。它将图像上传到服务器上。然而,我只能看到一个盒子。我哪里出错了?确保在最后转换回位图,即服务器端 1、将图像视图转换为位图 Bitmap bm = BitmapFactory.decodeResource(getResources(),R.drawable.image); ByteArrayOutputStream baos = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.JPEG,

这是我的密码。它将图像上传到服务器上。然而,我只能看到一个盒子。我哪里出错了?

确保在最后转换回位图,即服务器端

1、将图像视图转换为位图

Bitmap bm = BitmapFactory.decodeResource(getResources(),R.drawable.image);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object
    final String encodedImage = Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT);
byte[] decodedString = Base64.decode(Base64String.getBytes(), Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
2、将位图转换为base64字符串并传递给服务器

 imageView.buildDrawingCache();
 Bitmap bmap = imageView.getDrawingCache();
在服务器端,将base64转换为位图。 (这是java代码,用服务器端语言编写)


设置此位图以显示图像

将图像转换为位图

Bitmap bm = BitmapFactory.decodeResource(getResources(),R.drawable.image);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object
    final String encodedImage = Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT);
byte[] decodedString = Base64.decode(Base64String.getBytes(), Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
位图到字节[]

 File imagefile = new File(Environment.getExternalStorageDirectory(),"/image/test.jpg" );
    FileInputStream fis = null;
                try {
                    fis = new FileInputStream(imagefile);
                    } catch (FileNotFoundException e) {
                        logger.error(Log.getStackTraceString(e));
                    e.printStackTrace();
                }

  Bitmap bm = BitmapFactory.decodeStream(fis);
字节[]到字符串:

 ByteArrayOutputStream baos = new ByteArrayOutputStream();  
                bm.compress(Bitmap.CompressFormat.PNG, 10 , baos);    
                byte[] img = baos.toByteArray();

可能是重复的,最好也发布服务器端代码。我也尝试了Base64.NO\u WRAP。还是不起作用。不,我也检查过了。我使用了在线Base64转换器并使用了该字符串。成功了。我猜编码有问题。服务器端的字符串是否相同?使用一些日志并确认这不是POST方法的问题,当我这样做时,我在bm.compression上得到一个nullpointerexception变量bm为null。到达后,您必须在那里调试/登录并交叉检查imagefile是否必须提供映像的完整路径?是的,您需要提供完整路径,例如File imagefile=new File(Environment.getExternalStorageDirectory(),“/image/test.jpg”);交叉检查文件是否存在是的,我给出了完整的路径,并确保图像存在。我不知道为什么nullpointer异常仍然存在。