安卓存储资源

Posted jian-dan-ai-boke

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了安卓存储资源相关的知识,希望对你有一定的参考价值。

本地永久存储字符串

 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+"
"+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     }

 

以上是关于安卓存储资源的主要内容,如果未能解决你的问题,请参考以下文章

片段从一开始就没有显示 |安卓工作室

安卓存储资源

安卓。片段 getActivity() 有时返回 null

文件操作安卓8

安卓。通过从片段中的按钮调用片段中的方法来关闭片段?

如何在另一个片段中使用 YouTubePlayerFragment 加载 YouTubePlayer? (安卓)