【问题标题】:Convert String to byte[] in Android在Android中将字符串转换为字节[]
【发布时间】:2014-09-25 18:47:12
【问题描述】:

我想将路径图像的字符串转换为字节数组,而不是将其转换为位图。

我有错误:

09-25 09:38:11.050:
W/System.err(21261): java.lang.IllegalArgumentException: bad base-64
09-25 09:38:11.350: 
W/System.err(21261): at android.util.Base64.decode(Base64.java:161)
09-25 09:38:11.350: 
W/System.err(21261): at android.util.Base64.decode(Base64.java:136)
09-25 09:38:11.350:
W/System.err(21261): at com.up.upload.MainActivity.UploadImage(MainActivity.java:186)
09-25 09:38:11.350: 
W/System.err(21261): at com.up.upload.MainActivity$2.run(MainActivity.java:149)
09-25 09:38:11.350: 
W/System.err(21261): at java.lang.Thread.run(Thread.java:841)

我想我做得不对,但我知道为什么。

非常感谢您的帮助。

提前致谢

我的代码如下:

    String lStr = "/storage/emulated/0/image.jpg"

    byte[] data = Base64.decode(lStr.getBytes(), 0);

    Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);

    Request.addProperty("docbinaryarray", bmp);

【问题讨论】:

  • 您正试图将字符串的字节用作位图的数据,这很奇怪。您不应该在给定路径打开图像吗?除非你正在尝试一些扭曲的方式来传递隐藏的数据......
  • 你在做什么?图像的字符串路径不会生成位图图像

标签: android android-bitmap


【解决方案1】:

你做错了,伙计。您的代码只转换字符串,而不是来自该路径的图像数据。试试这个:

File imgFile = new  File("/storage/emulated/0/image.jpg");
if(imgFile.exists()){
    Bitmap bmp = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
    Request.addProperty("docbinaryarray", bmp);
}

【讨论】:

    【解决方案2】:

    你完全错了:

    用途:

    File mFile = new  File("/storage/emulated/0/image.jpg");
    if(mFile.exists()){
        Bitmap bmp = BitmapFactory.decodeFile(mFile.getAbsolutePath());
    }
    

    【讨论】:

      【解决方案3】:
      public static String encodeToBase64(String string)
       {
       String encodedString = "";
       try
       {
       byte[] byteData = null;
        if(Build.VERSION.SDK_INT >= 8) // Build.VERSION_CODES.FROYO --> 8
          {
       byteData = android.util.Base64.encode(string.getBytes(),android.util.Base64.DEFAULT);
      }
      else
      {
       byteData = Base64Utility.encode(string.getBytes(),Base64Utility.DEFAULT);
      }
       encodedString = new String(byteData);
      }
      catch (Exception e)
      {
       }
      return encodedString;
      }
      

      如果您使用的 API 级别

      【讨论】:

        【解决方案4】:

        如果您想从图像中获取字节数组 - 请尝试下一个代码:

         Bitmap bm = BitmapFactory.decodeFile("/path/image.jpg");
            ByteArrayOutputStream baos = new ByteArrayOutputStream();  
            bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object   
            byte[] b = baos.toByteArray(); 
        

        【讨论】:

          【解决方案5】:

          这样做

           File imgFile = new  File("/sdcard/Images/test_image.jpg");
              if(imgFile.exists()){
                  Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
          //for drawable 
                  //Drawable d = new BitmapDrawable(getResources(), myBitmap);
                  ImageView myImage = (ImageView) findViewById(R.id.imageview);
                  myImage.setImageBitmap(myBitmap);
          
              }
          

          【讨论】:

            猜你喜欢
            • 2013-05-14
            • 2019-10-10
            • 1970-01-01
            • 1970-01-01
            • 2022-01-18
            相关资源
            最近更新 更多