sd卡操作
private void writer() {
SharedPreferences mainActivity = getSharedPreferences("login", MODE_PRIVATE);
SharedPreferences.Editor edit = mainActivity.edit();
edit.putString("user","博客");
edit.putBoolean("isGame",true);
edit.putFloat("price",12);
edit.putLong("id",123456);
edit.putInt("age",13);
edit.commit();
}
private void read() {
SharedPreferences login = getSharedPreferences("login", MODE_PRIVATE);
String string = login.getString("user", "默认值");
int age=login.getInt("age",0);
boolean isMan=login.getBoolean("isGame",false);
float price=login.getFloat("price",0.0f);
long id=login.getLong("id",0l);
Toast.makeText(this, string+":"+age+":"+isMan+":"+price+":"+id, Toast.LENGTH_SHORT).show();
}
public class Main2Activity extends AppCompatActivity {
private SharedPreferences sharedPreferences;
private EditText username;
private EditText password;
private CheckBox cb;
private Button login;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
username = (EditText) findViewById(R.id.username);
password = (EditText) findViewById(R.id.password);
cb=(CheckBox)findViewById(R.id.cb_remember);
login=(Button)findViewById(R.id.login);
sharedPreferences = getSharedPreferences("name", MODE_PRIVATE);
boolean isclick=sharedPreferences.getBoolean("isclick",false);
if (isclick){
String username1 = sharedPreferences.getString("user", "默认值");
String password1 = sharedPreferences.getString("password", "默认密码");
username.setText(username1);
password.setText(password1);
cb.setChecked(true);
}
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username2 = username.getText().toString();
String password2 = password.getText().toString();
if(cb.isChecked()){
SharedPreferences.Editor edit = sharedPreferences.edit();
edit.putBoolean("isclick",true);
edit.putString("user",username2);
edit.putString("password",password2);
edit.commit();
}else{
SharedPreferences.Editor edit = sharedPreferences.edit();
edit.putBoolean("isclick",false);
edit.commit();
}
}
});
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Main2Activity"
android:orientation="vertical"
>
<EditText
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<RelativeLayout
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:layout_alignParentLeft="true"
android:id="@+id/cb_remember"
android:text="记住密码"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_alignParentRight="true"
android:id="@+id/login"
android:text="登录"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
</LinearLayout>
public class SDActivity extends AppCompatActivity {
private Button btn_sd;
private Button btn_read;
private Button btn_down;
private Button btn_up;
private ImageView iv_show;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sd);
String externalStorageState = Environment.getExternalStorageState();
Log.i("---", "onCreate: "+externalStorageState);
File externalStorageDirectory = Environment.getExternalStorageDirectory();
Log.i("---", "onCreate: "+externalStorageDirectory.toString());
File externalStoragePublicDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
Log.i("---", "onCreate: "+externalStoragePublicDirectory);
btn_sd=findViewById(R.id.btn_sd);
btn_read=findViewById(R.id.btn_read);
btn_down=findViewById(R.id.btn_down);
btn_up=findViewById(R.id.btn_up);
iv_show=findViewById(R.id.iv_show);
btn_down.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
write_bitmap("http://www.005.tv/uploads/allimg/181228/55-1Q22Q4152I93.jpg");
}
});
btn_up.setOnClickListener(new View.OnClickListener() {
Bitmap bitmap=null;
@Override
public void onClick(View v) {
File file = Environment.getExternalStorageDirectory();
try {
bitmap = BitmapFactory.decodeStream(new FileInputStream(new File(file, "jianmo.jpg")));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
iv_show.setImageBitmap(bitmap);
}
});
btn_read.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
File file = Environment.getExternalStorageDirectory();
File file1 = new File(file, "a.txt");
try {
FileInputStream fileInputStream = new FileInputStream(file1);
int len=0;
byte[] bys=new byte[1024];
while((len=fileInputStream.read(bys))!=-1){
String s = new String(bys, 0, len);
Toast.makeText(SDActivity.this, "", Toast.LENGTH_SHORT).show();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
btn_sd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
String[] str=new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE};
requestPermissions(str,100);
}
File file = Environment.getExternalStorageDirectory();
try {
FileOutputStream out = new FileOutputStream(new File(file, "a.txt"));
out.write("写入SD".getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode==110&&grantResults[0]== PackageManager.PERMISSION_GRANTED){
Log.i("---", "onRequestPermissionsResult: 同意");
}else {
Log.i("---", "onRequestPermissionsResult: 不同意");
}
}
public Bitmap read_bitmap(String filename){
Bitmap bitmap=null;
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
File file = Environment.getExternalStorageDirectory();
File file1 = new File(file, filename);
bitmap=BitmapFactory.decodeFile(file1.getAbsolutePath());
}
return bitmap;
}
public void write_bitmap(String urlstring){
new MyTask().execute(urlstring);
}
class MyTask extends AsyncTask<String,Void,String>{
@Override
protected String doInBackground(String... strings) {
FileOutputStream out=null;
InputStream inputStream=null;
HttpURLConnection connection=null;
try {
URL url= new URL(strings[0]);
connection= (HttpURLConnection) url.openConnection();
if (connection.getResponseCode()==200){
inputStream = connection.getInputStream();
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
File file = Environment.getExternalStorageDirectory();
out = new FileOutputStream(new File(file,"jianmo.jpg"));
byte[] bytes=new byte[1024];
int len=0;
while((len=inputStream.read(bytes))!=-1){
out.write(bytes,0,len);
}
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
}