欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

安卓存储资源

程序员文章站 2022-03-28 19:01:22
本地永久存储字符串 从网上下载图片信息然后存储到本地 ......

本地永久存储字符串

 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 block
10             e.printstacktrace();
11         }
12         try {
13             fwriter.flush();
14             fwriter.close();
15         } catch (ioexception e) {
16             // todo auto-generated catch block
17             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 block
38             e.printstacktrace();
39         }finally{
40             if(reader!=null)
41             {
42                 try {
43                     reader.close();
44                 } catch (ioexception e) {
45                     // todo auto-generated catch block
46                     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     }