2012-03-05 155 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.