专高一Day8

今天主要学习了SharedPreferences

先要给模拟器授权

然后学习了SDK的增删改查

先加入数据

先获取对象

然后获取编辑

然后数据处理

最后提交数据

查:

修:

public void update(View view) {//修改

//获取对象

SharedPreferences sharedPreferences=getSharedPreferences("dhg",MODE_PRIVATE);

//获取编辑

SharedPreferences.Editor editor=sharedPreferences.edit();

//数据处理

editor.putString("name","细狗");

//提交

editor.commit();

}

删除:

public void remove(View view) {

//获取sp

SharedPreferences sharedPreferences=getSharedPreferences("dhg",MODE_PRIVATE);

//获取编辑

SharedPreferences.Editor editor=sharedPreferences.edit();

//数据处理

editor.remove("name");

//提交

editor.commit();

}

外部储存:

MODE_PRIVATE:默认操作模式,代表该文件是私有数据,只能被应用本身访问

MODE_APPEND:检查文件是否存在,存在就往文件追加内容,否则就创建新文件。

MODE_WORLD_WRITEABLE:允许其他应用写入数据到该文件。

MODE_WORLD_READABLE:允许其他应用读取该文件。

Environment.getExternalStorageState() 判断SD卡状态

Environment.getExternalStorageDirectory();获取SD卡的根目录

Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)获取SD卡公开目录pictures文件夹

文件储存:

public void write(View view) throws FileNotFoundException {

if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))

{

File path=Environment.getExternalStorageDirectory();

File file=new File(path,"2012.txt");

try {

FileOutputStream fileOutputStream = new FileOutputStream(file);

fileOutputStream.write("你好大黑狗".getBytes());

} catch (IOException e) {

e.printStackTrace();

}

}

}

外部储存-图片储存

public void downimage(View view) {

new TaskImage().execute("http://43.143.146.165:8080/class5server/image/9.jpg");

}

class TaskImage extends AsyncTask<String,Void,String>{

@Override

protected String doInBackground(String... strings) {

try {

URL url = new URL(strings[0]);

HttpURLConnection httpURLConnection= (HttpURLConnection) url.openConnection();

httpURLConnection.setRequestMethod("GET");

httpURLConnection.setConnectTimeout(10*1000);

httpURLConnection.setReadTimeout(10*1000);

httpURLConnection.connect();

if(httpURLConnection.getResponseCode()==200)

{

InputStream inputStream = httpURLConnection.getInputStream();

if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))

{

int len=0;

byte[] buffer=new byte[1024];

File path=Environment.getExternalStorageDirectory();

File file=new File(path,"dhg.jpg");

FileOutputStream fileOutputStream=new FileOutputStream(file);

while ((len=inputStream.read(buffer))!=-1)

{

fileOutputStream.write(buffer,0,len);

}

}

}

} catch (MalformedURLException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

return null;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值