本地永久存储字符串
1 public void saveAsFile(String content) 2 { 3 String headPath=android.os.Environment.getExternalStorageDirectory()+"/aaa/mdoor.txt"; 4 FileWriter fwriter=null; 5 try { 6 fwriter=new FileWriter(headPath); 7 fwriter.write(content); 8 } catch (IOException e) { 9 // TODO Auto-generated catch block10 e.printStackTrace();11 }12 try {13 fwriter.flush();14 fwriter.close();15 } catch (IOException e) {16 // TODO Auto-generated catch block17 e.printStackTrace();18 }19 }20 public void readFile()21 {22 String headPath=android.os.Environment.getExternalStorageDirectory()+"/aaa/mdoor.txt";23 File file = new File(headPath);24 BufferedReader reader=null;25 try {26 reader= new BufferedReader(new FileReader(file));27 String tempString =null;28 29 while((tempString=reader.readLine())!=null)30 {31 Log.e("paytest","json"+tempString );32 content=tempString;33 }34 35 reader.close();36 } catch (Exception e) {37 // TODO Auto-generated catch block38 e.printStackTrace();39 }finally{40 if(reader!=null)41 {42 try {43 reader.close();44 } catch (IOException e) {45 // TODO Auto-generated catch block46 e.printStackTrace();47 }48 }49 }50 }
从网上下载图片信息然后存储到本地
1 public void savePIC() 2 { 3 Bitmap bitmap = null; 4 try { 5 6 bitmap =getPicture("https://qmby.feefoxes.com/h5/newlegend/version1/wechatapp/sharetest.jpg"); 7 } catch (Exception e) { 8 // TODO Auto-generated catch block 9 e.printStackTrace(); 10 Log.e("paytest", "aaa"); 11 } 12 try { 13 saveBitmap(bitmap); 14 } catch (Exception e) { 15 // TODO Auto-generated catch block 16 e.printStackTrace(); 17 Log.e("paytest", "hhh"); 18 } 19 } 20 public void saveBitmap(Bitmap bitmap) throws IOException { 21 //更改的名字 22 String imageName="wwwwa"+".jpg"; 23 24 String headPath=android.os.Environment.getExternalStorageDirectory()+"/aaa"; 25 Log.e("headPath", headPath); 26 Log.e("headPath", headPath); 27 File headDir=new File(headPath); 28 if(!headDir.exists()){ 29 headDir.mkdirs(); 30 } 31 System.out.println(headPath+"\n"+headDir); 32 FileOutputStream headFos=null; 33 File headFile=null; 34 try{ 35 //重命名并保存 36 headFile=new File(headPath,imageName); 37 headFile.createNewFile(); 38 39 headFos=new FileOutputStream(headFile); 40 bitmap.compress(CompressFormat.JPEG, 100, headFos); 41 headFos.flush(); 42 43 }catch(Exception e){ 44 e.printStackTrace(); 45 }finally{ 46 if(headFos!=null){ 47 try { 48 headFos.close(); 49 } catch (IOException e) { 50 e.printStackTrace(); 51 } 52 } 53 } 54 55 } 56 /* 57 * 从服务器端得到图片 58 */ 59 public Bitmap getPicture(String path) throws Exception 60 { 61 Bitmap bm=null; 62 URL url; 63 try { 64 SSLSocketFactory.getSocketFactory().setHostnameVerifier(new AllowAllHostnameVerifier()); 65 url = new URL(path);//创建URL对象 66 URLConnection conn=url.openConnection();//获取URL对象对应的连接 67 conn.connect();//打开连接 68 InputStream is=conn.getInputStream();//获取输入流对象 69 /*String filepath=null; 70 if(numberPic==1) 71 { 72 filepath="tempDir"+numberPic+".png"; 73 }else if(numberPic==2) 74 { 75 filepath="tempDir"+numberPic+".jpg"; 76 } 77 byte[]data =readInputStream(is); 78 79 File imageFile =new File(filepath); 80 81 FileOutputStream outStream = new FileOutputStream(imageFile); 82 83 outStream.write(data); 84 85 86 Log.e("data", ""+outStream); 87 Log.e("data", ""+outStream); 88 */ 89 bm=BitmapFactory.decodeStream(is);//根据输入流对象创建Bitmap对象 90 // outStream.close(); 91 // is.close(); 92 } catch (MalformedURLException e1) { 93 e1.printStackTrace();//输出异常信息 94 Log.e("paytest",""+"url"); 95 }catch (Exception e) { 96 e.printStackTrace();//输出异常信息 97 Log.e("paytest",""+"bb"); 98 } 99 return bm;100 }
原文出处:https://www.cnblogs.com/jian-dan-ai-boke/p/9609919.html
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦