2012-03-05 93 views
0

我试图在Android中从我的服务器发送的XML文件中获取资源。它直接以Base64格式向我发送一个PNG文件。动态创建PNG资源

这样的:

lgrbehzlgrbezgbrhezugizgbrzbgre

我的问题是:

有没有使用“R.drawable.MyNewResource”从我的服务器发送作为资源类的PNG数据的方法?

或者我可以从我的应用程序数据文件夹中的PNG文件设置ImageResoure?

在此先感谢!

回答

1

好吧,你的问题是不妥当的,如果我没看错的,那么你想要的东西一样,

只是转换你的网络响应base64 png string into bitmap,然后使用位图在imageview的设置。

Bitmap bitmap;   
ImageView imgView; 
byte[] decodedString;  

decodedString = Base64.decode(encodedImage, Base64.DEFAULT); 
bitmap = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); 
imgView.setImageBitmap(bitmap); 
+0

对不起,忘记提到了,encodedImage是一个字符串字段,它包含.png文件的base64字符串。 – user370305 2012-03-05 11:31:46

+0

感谢您的答案,但它似乎像我的PNG文件不正确创建的位图工厂。 mHeight和mWidth都等于“-1”,你有什么线索的问题? – Thordax 2012-03-07 15:29:00

+0

好吧,我的坏,这是因为Base64类型的错误(在我的情况下NO_WRAP而不是DEFAULT) – Thordax 2012-03-07 15:36:31

0
You can create file and write your bitmap in file using this code. 

File file=new File(imageDirectory,image_name); 
      OutputStream outStream=null; 
      try 
      { 
        outStream = new FileOutputStream(file); 
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream); 
        outStream.flush(); 
        outStream.close(); 

        Toast.makeText(cameraActivity.this, "Saved", Toast.LENGTH_LONG).show(); 

      } 

      catch (FileNotFoundException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
        Toast.makeText(cameraActivity.this, e.toString(), Toast.LENGTH_LONG).show(); 
        } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
        Toast.makeText(cameraActivity.this, e.toString(), Toast.LENGTH_LONG).show(); 
        } 

Bitmap will be saved in application file and then you can read it.