文件存入和读取
内存与外存读取
package com.bw.a622lx;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.Button;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashSet;
import java.util.Set;
import javax.net.ssl.HttpsURLConnection;
public class MainActivity extends AppCompatActivity {
private String url="https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=1141259048,554497535&fm=26&gp=0.jpg";
private Button txt1;
private ImageView img1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt1 = (Button) findViewById(R.id.txt1);
img1 = (ImageView) findViewById(R.id.img1);
saveDate();
readDate();
saveFile();
readFile();
new Thread(new Runnable() {
@Override
public void run() {
DownLoadPic();
}
}).start();
}
public void saveDate(){
SharedPreferences sharedPreferences = getSharedPreferences("1804AFile",MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putString("String","哈哈");
editor.putBoolean("bool",true);
Set<String> strings=new HashSet<>();
strings.add("嘻嘻");
strings.add("哈哈");
strings.add("嘻嘻哈哈");
editor.putStringSet("strings",strings);
editor.commit();
}
public void readDate(){
SharedPreferences sharedPreferences = getSharedPreferences("1804AFile",MODE_PRIVATE);
String str=sharedPreferences.getString("String",null);
Set<String> sets=sharedPreferences.getStringSet("strings",null);
Log.i("---",str);
for (String s:sets){
Log.i("---",s);
}
}
public void saveFile(){
if (Environment.getExternalStorageState().equals
(Environment.MEDIA_MOUNTED)) {
try {
String sdpath=Environment.getExternalStorageDirectory().getPath();
File file = Environment.getExternalStorageDirectory();
FileOutputStream fileOutputStream=new FileOutputStream(sdpath+"/a.txt");
fileOutputStream.write("这就是对了".getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void readFile(){
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
File file = new File(Environment.getExternalStorageDirectory().getPath() + "/a.txt");
try {
FileInputStream fileInputStream=new FileInputStream(file);
byte[] bytes=new byte[1024];
int len=0;
StringBuffer buffer=new StringBuffer();
while((len=fileInputStream.read(bytes))!=-1){
buffer.append(new String(bytes,0,len));
}
Log.i("---","这是读出来的"+buffer.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void DownLoadPic(){
try {
URL url = new URL(this.url);
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
if (connection.getResponseCode()==200){
InputStream inputStream = connection.getInputStream();
int len=0;
byte[] bytes=new byte[1024];
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
String path = Environment.getExternalStorageDirectory().getPath();
File file=new File(path+"/p.jpg");
FileOutputStream fileOutputStream=new FileOutputStream(file);
while((len=inputStream.read(bytes))!=-1){
fileOutputStream.write(bytes,0,len);
}
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void readImage(){
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
String path = Environment.getExternalStorageDirectory().getPath();
File file=new File(path+"/p.jpg");
try {
FileInputStream inputStream=new FileInputStream(file);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
img1.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
}