您的位置:首页 > 移动开发 > Android开发

android一些方法

2015-11-03 15:15 417 查看
1 传递对象给fragment

bundle = new Bundle();

bundle.putInt(FacePageFragment.ARG_POSITION, position);

bundle.putSerializable(FacePageFragment.ARG_FACE_DATA,

data.get(data.keySet().toArray()[position]));

fragment.setArguments(bundle);

fragment.setArguments(bundle);

2/*

* 这个是解压ZIP格式文件的方法

*

* @zipFileName:是传进来你要解压的文件路径,包括文件的名字;

*

* @outputDirectory:选择你要保存的路劲;

*/

public static void unzip(String zipFileName, String outputDirectory)

throws Exception {

ZipInputStream in = new ZipInputStream(new FileInputStream(zipFileName));

try {

ZipEntry z;

String name = "";

while ((z = in.getNextEntry()) != null) {

name = z.getName();

Log.d("unzip", "unzipping file: " + name);

if (z.isDirectory()) {

Log.d("unzip", name + "is a folder");

// get the folder name of the widget

name = name.substring(0, name.length() - 1);

File folder = new File(outputDirectory + File.separator

+ name);

folder.mkdirs();

Log.d("unzip", "mkdir " + outputDirectory + File.separator

+ name);

} else {

Log.d("unzip", name + "is a normal file");

File file = new File(outputDirectory + File.separator

+ name);

file.createNewFile();

// get the output stream of the file

FileOutputStream out = new FileOutputStream(file);

int ch;

byte[] buffer = new byte[1024];

// read (ch) bytes into buffer

while ((ch = in.read(buffer)) != -1) {

// write (ch) byte from buffer at the position 0

out.write(buffer, 0, ch);

out.flush();

}

out.close();

}

}

} catch (Exception e) {

// TODO: handle exception

} finally {

if (in != null) {

in.close();

}

}

}

注:压缩包要是zip格式

3 解析.json文件,里面是json格式的字符串

/**

* 解析图片

*

* @param context

* @param reelBean

* @return

*/

public static List<GifEmotionBean> parserBean(

Context context, String path) {

List<GifEmotionBean> profileImageBeans = new ArrayList<GifEmotionBean>();

BufferedReader reader = null;

if (path == null) {

return null;

}

InputStreamReader is = null;

try {

if (path.contains("assets")) {

String pathString = path.replace("assets/", "");

is = new InputStreamReader(context.getResources().getAssets()

.open(pathString));

reader = new BufferedReader(is);

} else {

File file = new File(path);

is = new InputStreamReader(new FileInputStream(file) , "GBK");

reader = new BufferedReader(is);

}

StringBuilder sb = new StringBuilder();

String line = null;

if (reader != null) {

while ((line = reader.readLine()) != null) {

sb.append(line + " ");

}

}

String json = sb.toString();

JSONArray array = new JSONArray(json);

for (int i = 0; i < array.length(); i++) {

JSONObject object = array.getJSONObject(i);

GifEmotionBean bean = new GifEmotionBean(object);

profileImageBeans.add(bean);

}

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (JSONException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

if (reader != null) {

try {

reader.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

if (is != null) {

try {

is.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

return profileImageBeans;

}

返回的是json里面的一些数据,封装在bean里面
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: