2012-05-09 51 views
1

我正在通过ajax检索图片文件为二进制代码,然后javascript将其传递给java上的android和android正在获取二进制代码,但我不能让它保存...我尝试了很多不同的方式,没有任何作用。java将二进制代码保存到文件

上的Java代码到目前为止是这样的:

public void setFile(String sFileName, String sBody){ 
    try 
    { 
     //Log.w("setfile", sBody); 
     File root = new File(Environment.getExternalStorageDirectory(), local_address); 
     if (!root.exists()) { 
      root.mkdirs(); 
     } 
     File gpxfile = new File(root, sFileName); 
     FileOutputStream aFileOutStream = new FileOutputStream(gpxfile); 
     DataOutputStream aDataOutputStream = new DataOutputStream(aFileOutStream); 


     aDataOutputStream.writeUTF(sBody); 
     aDataOutputStream.flush(); 
     aDataOutputStream.close(); 
     aFileOutStream.close(); 

     //FileWriter writer = new FileWriter(gpxfile); 
     //writer.append(sBody); 
     //writer.flush(); 
     //writer.close(); 
     //Toast.makeText(this, "Saved", Toast.LENGTH_SHORT).show(); 
    } 
    catch(IOException e) 
    { 
     e.printStackTrace(); 
    } 
    } 

我知道Java是工作,因为如果我取消这条线

//Log.w("setfile”,sBody);

日志猫将返回的JavaScript发送的java

+0

你在你的本地文件得到了什么呢?任何特定的错误消息? – ametren

+0

它是'日志猫'不'低猫'x) – sschrass

+0

不,该文件是空的okb – user1342645

回答

0

我相信你想使用writeBytes(),而不是writeUTF()所以它不会改变你的编码。

+0

做到了,并没有工作 – user1342645

0

这个工作对我来说:

public void WriteSettings(Context context, String data){ 
    FileOutputStream fOut = null; 
    OutputStreamWriter osw = null; 

    try{ 
     fOut = openFileOutput("settings.dat", MODE_PRIVATE);  
     osw = new OutputStreamWriter(fOut); 

     osw.write(data); 
     osw.flush(); 

     Toast.makeText(context, "Settings saved", Toast.LENGTH_SHORT).show(); 
    } 

     catch (Exception e) {  
     e.printStackTrace(); 
     Toast.makeText(context, "Settings not saved", Toast.LENGTH_SHORT).show(); 
     } 

     finally { 
     try { 
      osw.close(); 
      fOut.close(); 
     } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
}