今天主要学习了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;
}
}