android图片存储和读取,草稿箱功能

来源:互联网 发布:算法第四版的配套网站 编辑:程序博客网 时间:2024/05/29 02:38

2015年8月27日 工作日记
增加了发帖页面草稿箱功能,用户按到左上角的返回按钮的时候,容易丢失了曾经编辑的内容,所以我们的功能是将文字及图片放入SharedPreferences 里。文字还好放,主要是图片路径。
首先是SharedPreferences的应用:

        String title = postTitle.getText().toString().trim();        String content = postContent.getText().toString().trim();        SharedPreferences mSharedPreferences = getSharedPreferences("SaveUncompletePost", 0);        SharedPreferences.Editor mEditor = mSharedPreferences.edit();          mEditor.putString("title", title);          mEditor.putString("content", content);          List<String> uncompletePotoPath = new ArrayList<String>();        for (PhotoModel photo : selected) {            uncompletePotoPath.add(photo.getOriginalPath());        }        String jsonpathString = new JSONArray(uncompletePotoPath).toString();        mEditor.putString("pictures", jsonpathString);        mEditor.putBoolean("isEmpty", false);        mEditor.commit();

其中图片存储的是图片的路径,将字符串列表转换成json字符串
String jsonpathString = new JSONArray(list).toString();

在再次打开发帖页面的时候,读取曾经存取的图片路径

JSONArray jsArr = new JSONArray(jsonPathString);                    if(jsArr != null) for (int i = 0; i < jsArr.length(); i++) {                         String picturepath = (String)jsArr.get(i);                         PhotoModel photo = new PhotoModel(picturepath,true);                         if(photo!=null){                             selected.add(photo);                             //假地址,为了解决寻找地址空的问题                             Integer pInteger= new Integer(5);                             positionList.add(pInteger);                         }                    }

首先将json字符串转换回jsonArray,然后一个一个读取里边的字符串。
JSONArray jsArry = new JSONArray(jsonString)l;
依次遍历数组强制转换类型 String path= jsArry.get(i);

0 0