志不强者智不达,言不信者行不果。这篇文章主要讲述安卓存储资源相关的知识,希望能为你提供帮助。
本地永久存储字符串
1 public void saveAsFile(String content) 2{ 3String headPath=android.os.Environment.getExternalStorageDirectory()+"/aaa/mdoor.txt"; 4FileWriter fwriter=null; 5try { 6fwriter=new FileWriter(headPath); 7fwriter.write(content); 8} catch (IOException e) { 9// TODO Auto-generated catch block 10e.printStackTrace(); 11} 12try { 13fwriter.flush(); 14fwriter.close(); 15} catch (IOException e) { 16// TODO Auto-generated catch block 17e.printStackTrace(); 18} 19} 20public void readFile() 21{ 22String headPath=android.os.Environment.getExternalStorageDirectory()+"/aaa/mdoor.txt"; 23File file = new File(headPath); 24BufferedReader reader=null; 25try { 26reader= new BufferedReader(new FileReader(file)); 27String tempString =null; 28 29while((tempString=reader.readLine())!=null) 30{ 31Log.e("paytest","json"+tempString ); 32content=tempString; 33} 34 35reader.close(); 36} catch (Exception e) { 37// TODO Auto-generated catch block 38e.printStackTrace(); 39}finally{ 40if(reader!=null) 41{ 42try { 43reader.close(); 44} catch (IOException e) { 45// TODO Auto-generated catch block 46e.printStackTrace(); 47} 48} 49} 50}
从网上下载图片信息然后存储到本地
1 public void savePIC() 2{ 3Bitmap bitmap = null; 4try { 5 6bitmap =getPicture("https://qmby.feefoxes.com/h5/newlegend/version1/wechatapp/sharetest.jpg"); 7} catch (Exception e) { 8// TODO Auto-generated catch block 9e.printStackTrace(); 10Log.e("paytest", "aaa"); 11} 12try { 13saveBitmap(bitmap); 14} catch (Exception e) { 15// TODO Auto-generated catch block 16e.printStackTrace(); 17Log.e("paytest", "hhh"); 18} 19} 20public void saveBitmap(Bitmap bitmap) throws IOException { 21//更改的名字 22String imageName="wwwwa"+".jpg"; 23 24String headPath=android.os.Environment.getExternalStorageDirectory()+"/aaa"; 25Log.e("headPath", headPath); 26Log.e("headPath", headPath); 27File headDir=new File(headPath); 28if(!headDir.exists()){ 29headDir.mkdirs(); 30} 31System.out.println(headPath+" "+headDir); 32FileOutputStream headFos=null; 33File headFile=null; 34try{ 35//重命名并保存 36headFile=new File(headPath,imageName); 37headFile.createNewFile(); 38 39headFos=new FileOutputStream(headFile); 40bitmap.compress(CompressFormat.JPEG, 100, headFos); 41headFos.flush(); 42 43}catch(Exception e){ 44e.printStackTrace(); 45}finally{ 46if(headFos!=null){ 47try { 48headFos.close(); 49} catch (IOException e) { 50e.printStackTrace(); 51} 52} 53} 54 55} 56/* 57* 从服务器端得到图片 58*/ 59public Bitmap getPicture(String path) throws Exception 60{ 61Bitmap bm=null; 62URL url; 63try { 64SSLSocketFactory.getSocketFactory().setHostnameVerifier(new AllowAllHostnameVerifier()); 65url = new URL(path); //创建URL对象 66URLConnection conn=url.openConnection(); //获取URL对象对应的连接 67conn.connect(); //打开连接 68InputStream is=conn.getInputStream(); //获取输入流对象 69/*String filepath=null; 70if(numberPic==1) 71{ 72filepath="tempDir"+numberPic+".png"; 73}else if(numberPic==2) 74{ 75filepath="tempDir"+numberPic+".jpg"; 76} 77byte[]data =https://www.songbingjia.com/android/readInputStream(is); 78 79File imageFile =new File(filepath); 80 81FileOutputStream outStream = new FileOutputStream(imageFile); 82 83outStream.write(data); 84 85 86Log.e("data", ""+outStream); 87Log.e("data", ""+outStream); 88*/ 89bm=BitmapFactory.decodeStream(is); //根据输入流对象创建Bitmap对象 90 //outStream.close(); 91 //is.close(); 92} catch (MalformedURLException e1) { 93e1.printStackTrace(); //输出异常信息 94Log.e("paytest",""+"url"); 95}catch (Exception e) { 96e.printStackTrace(); //输出异常信息 97Log.e("paytest",""+"bb"); 98} 99return bm; 100}
【安卓存储资源】
推荐阅读
- appium windows环境环境
- Android APK 包名以及Activity 查询方式
- appium mac环境搭建
- CSAPP —— 信息的表示和处理
- form编码方式application/x-www-form-urlencoded和multipart/form-data的区别
- 2_如何将android虚拟设备的语言设置为简体中文
- 从0开始编译安卓 9.0
- Android多媒体框架
- Xamarin.Forms中的单元格用法示例图解