**
一、SD卡操作
**
一般手机文件管理 根路径 /storage/emulated/0/或者/mnt/shell/emulated/0
重要代码:
(1)Environment.getExternalStorageState();// 判断SD卡是否
(2)Environment.getExternalStorageDirectory(); 获取SD卡的根目录
(3)Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); 获取SD卡公开目录pictures文件夹
必须要添加读写SD卡的权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
实现步骤
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button writer;
Button read;
Button download;
Button read_pic;
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
writer = findViewById(R.id.write);
read = findViewById(R.id.read);
writer.setOnClickListener(this);
read.setOnClickListener(this);
download = findViewById(R.id.download);
read_pic = findViewById(R.id.read_pic);
download.setOnClickListener(this);
read_pic.setOnClickListener(this);
imageView = findViewById(R.id.img);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.write:
bt_writer();
break;
case R.id.read:
bt_read();
break;
case R.id.download:
bt_donwload();
break;
case R.id.read_pic:
bt_read_pic();
break;
}
}
private void bt_read_pic() {
imageView.setImageBitmap(BitmapFactory.decodeFile("/sdcard/Download/小恶魔.png"));
}
//网上下载
private void bt_donwload() {
new My_THread().start();
}
//读取数据
private void bt_read() {
//获取SD卡的0路径
File file = Environment.getExternalStorageDirectory();
File download = new File(file, "Download");
File myfile = new File(download, "秘密文件.txt");
//创建流对象(读取创建输入流)
FileInputStream fis = null;
try {
fis = new FileInputStream(myfile);
byte[] bytes = new byte[fis.available()];
fis.read(bytes);
String s = new String(bytes, 0, bytes.length);
Toast.makeText(this, s, Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//写入数据
private void bt_writer() {
//获取SD卡的0路径
File file = Environment.getExternalStorageDirectory();
File download = new File(file, "Download");
File myfile = new File(download, "秘密文件.txt");
//创建流对象(写入创建输出流)
FileOutputStream fos = null;
try {
fos = new FileOutputStream(myfile);
fos.write("大家好我是王震".getBytes());
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
二、三级缓存
三级缓存的优点:节省流量+支持离线浏览+速度快
首先要明确什么时三级缓存
从内存获取图片, 如果存在, 则显示; 如果不存在, 则从SD卡中获取图片
从SD卡中获取图片, 如果文件中存在, 显示, 并且添加到内存中; 否则开启网络下载图片
从网络下载图片, 如果下载成功, 则添加到缓存中, 存入SD卡, 显示图片
必须要添加读写SD卡的权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
三个工具类(重点)
1.操作内存工具类
/*
内存缓存图片工具类
主要是以键值对存储
*/
public class LruUtlis {
//获取最大内存
static long maxsize = Runtime.getRuntime().maxMemory();
//获取内存对象
static LruCache<String,Bitmap> lruCache = new LruCache<String, Bitmap>((int) (maxsize/8)){//设置给最大内存的1/8
@Override
//重写方法返回图片对象的大小
protected int sizeOf(String key, Bitmap value) {
return value.getByteCount();
}
};
public static void setBitmap(String key,Bitmap bitmap){
lruCache.put(key,bitmap);
}
public static Bitmap getBitmap(String key){
return lruCache.get(key);
}
}
2.操作SD卡工具类
**
* 1.从SD读取 根据路径---->Bitmap.compress() 压缩
* 2.SD写 bitmap--->路径 BitmapFactory.decodeFile(文件路径)
*
* */
public class SDUtils {
//写入SD卡图片操作
//参数一:文件路径 参数二:bitmap对象
public static void setBitmap(String filepath, Bitmap bitmap){
//判断SD卡是否挂载
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
//参数一 图片的格式 JPG PNG 参数二 质量 0-100 参数三 输出流
try {
bitmap.compress(Bitmap.CompressFormat.JPEG,100,new FileOutputStream(filepath));
} catch (Exception e) {
e.printStackTrace();
}
}
}
//读取SD卡图片操作
public static Bitmap getBitmap(String filepath){
//判断SD卡是否挂载
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
Bitmap bitmap = BitmapFactory.decodeFile(filepath);
return bitmap;
}
return null;
}
}
3.操作网络工具类
网络下载不能在主线程
/*
网络缓存图片
*/
public class NetUtils {
public static Bitmap getBitmap(String url){
Bitmap bitmap = null;
try {
bitmap = new MyTask().execute(url).get();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return bitmap;
}
static class MyTask extends AsyncTask<String,Object,Bitmap>{
@Override
protected Bitmap doInBackground(String... strings) {
try {
URL url = new URL(strings[0]);
HttpURLConnection hcon = (HttpURLConnection) url.openConnection();
if (hcon.getResponseCode() == 200){
InputStream is = hcon.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(is);
return bitmap;
}
} catch (Exception e) {
e.printStackTrace();
}
return null ;
}
}
}
主线程代码
public class MainActivity extends AppCompatActivity {
Button button;
ImageView img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
img = findViewById(R.id.img);
button.setOnClickListener(new View.OnClickListener() {
Bitmap bitmap;
@Override
public void onClick(View v) {
bitmap = LruUtlis.getBitmap("海绵宝宝.jpg");
if (bitmap == null){
bitmap = SDUtils.getBitmap("/sdcard/Download/海绵宝宝.jpg");
if (bitmap == null){
bitmap = NetUtils.getBitmap("http://pic1.win4000.com/wallpaper/e/50d80458e1373.jpg");
if (bitmap == null){
Toast.makeText(MainActivity.this, "没流量了", Toast.LENGTH_SHORT).show();
}else {
img.setImageBitmap(bitmap);
SDUtils.setBitmap("/sdcard/Download/海绵宝宝.jpg",bitmap);
LruUtlis.setBitmap("海绵宝宝.jpg",bitmap);
Toast.makeText(MainActivity.this, "从网络获取的图片", Toast.LENGTH_SHORT).show();
}
}else {
LruUtlis.setBitmap("海绵宝宝.jpg",bitmap);
img.setImageBitmap(bitmap);
Toast.makeText(MainActivity.this, "从SD获取的图片", Toast.LENGTH_SHORT).show();
}
}else {
img.setImageBitmap(bitmap);
Toast.makeText(MainActivity.this, "从内存获取的图片", Toast.LENGTH_SHORT).show();
}
}
});
}
}
三、Bitmap二次采样(尺寸压缩)
尺寸压缩的优点:生成缩略图的时候,防止OOM内存溢出
//第一次采样
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(list.get(position),options);
//获取宽高
int outWidth = options.outWidth;
int outHeight = options.outHeight;
int size = 1;
while (outWidth/size>300||outHeight/size>300){
size*=2;
}
//二次采样
options.inJustDecodeBounds = false;
options.inSampleSize = size;
Bitmap bitmap = BitmapFactory.decodeFile(list.get(position), options);
四、Bitmap质量压缩
质量压缩的优点:节省SD卡的内存
//参数一 图片的格式 JPG PNG 参数二 质量 0-100 参数三 输出流
try {
bitmap.compress(Bitmap.CompressFormat.JPEG,100,new FileOutputStream(filepath));
} catch (Exception e) {
e.printStackTrace();
}