2014年9月22日 星期一

Android-筆記 讀取 Asset 資料夾的 Json 檔

 
public static void copyProtectionLatlonJson(Context context)
 {
  try 
  {
   InputStream mInput = context.getAssets().open("protection.json");
   if(mInput == null || mInput.equals(""))
   {
    throw new RuntimeException("Inputstream is null");
   }
   else
   {
    String content = InputStreamTOString(mInput);
    if (content != null)
    {
     AESharedPreferences.saveProtectionLatlonJson(context, content);
    }
   }
   mInput.close();
  } 
  catch (IOException e) 
  {
   e.printStackTrace();
  }
 }
 
 private final static int BUFFER_SIZE = 4096*4;
 private static String InputStreamTOString(InputStream in)
 {  
  try 
  {
         ByteArrayOutputStream outStream = new ByteArrayOutputStream();  
         byte[] data = new byte[BUFFER_SIZE];  
         int count = -1;  
   while((count = in.read(data,0,BUFFER_SIZE)) != -1)  
   {
    outStream.write(data, 0, count); 
   }
   data = null;  
         return new String(outStream.toByteArray(),"ISO-8859-1");
  } 
  catch (IOException e) 
  {
   e.printStackTrace();
  }
  return "";
    }

沒有留言:

張貼留言