Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.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:如何从字符串创建位图?_Java_Android_Bitmap - Fatal编程技术网

Java Android:如何从字符串创建位图?

Java Android:如何从字符串创建位图?,java,android,bitmap,Java,Android,Bitmap,我有一些字符串中的文本,我想保存为图像。现在我有以下代码: private void saveImage(View view){ String mPath = Environment.getExternalStorageDirectory().toString() + "/" + "spacitron.png"; String spacitron = "spacitron"; Bitmap bitmap = BitmapFactory.decodeByteArray(

我有一些字符串中的文本,我想保存为图像。现在我有以下代码:

private void saveImage(View view){
    String mPath = Environment.getExternalStorageDirectory().toString() + "/" + "spacitron.png";  
    String spacitron = "spacitron";
    Bitmap bitmap = BitmapFactory.decodeByteArray(spacitron.getBytes(), 0, spacitron.getBytes().length*5);
    OutputStream fout = null;
    File imageFile = new File(mPath);

    try {
        fout = new FileOutputStream(imageFile);
        //This line throws a null pointer exception
        bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fout);
        fout.flush();
        fout.close();
        } catch (FileNotFoundException e) {
        } catch (IOException e) {
        }
    }

但是,这不会创建位图,而是引发空指针异常。如何将字符串保存到位图?

在Android中,我需要多次将位图转换为字符串和字符串转换为位图。向服务器发送或接收位图并将图像存储在数据库中时

Bitmap to String:
public String BitMapToString(Bitmap bitmap){
            ByteArrayOutputStream baos=new  ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG,100, baos);
            byte [] arr=baos.toByteArray();
            String result=Base64.encodeToString(arr, Base64.DEFAULT);
            return result;
      }


String to Bitmap:
public Bitmap StringToBitMap(String image){
       try{
           byte [] encodeByte=Base64.decode(image,Base64.DEFAULT);
           Bitmap bitmap=BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
           return bitmap;
         }catch(Exception e){
           e.getMessage();
          return null;
         }
 }

创建画布对象并在画布上绘制文本。然后将画布另存为位图

Bitmap toDisk = Bitmap.createBitmap(w,h,Bitmap.Config.ARGB_8888);
canvas.setBitmap(toDisk);


canvas.drawText(message, x , y, paint);

toDisk.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(new File("/mnt/sdcard/viral.jpg")));

是字符串,位图的代码?或者位图应该是包含字符串的位图?这很接近,但我得到一个空白位图。尝试暂时显示画布,看看它是否可以写入文本。x和y非常重要。确保在设备的宽度和高度范围内绘制文本。当我打印StringToBitMap的堆栈轨迹时,我收到一个IllegalArgumentException。上面写着“坏垒64”。我试图显示一个数字,并将其转换为位图,以便居中。我尝试了“0”,但我也尝试了“Hello”和其他几个字符串,它仍然给了我那个错误。