<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical"
android:padding="10dp" >
<Button
android:id="@+id/btn_delete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="删除所有图片文件"
android:textColor="@color/black"
android:textSize="20sp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp" >
<TextView
android:id="@+id/tv_file"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:gravity="center"
android:text="文件名:"
android:textColor="@color/black"
android:textSize="17sp" />
<Spinner
android:id="@+id/sp_file"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="@+id/tv_file"
android:gravity="left|center"
android:spinnerMode="dialog" />
</RelativeLayout>
<ImageView
android:id="@+id/iv_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitCenter" />
</LinearLayout>
package com.example.forth;
import android.graphics.Bitmap;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;
import java.io.File;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private final static String TAG = "ImageReadActivity";
private ImageView iv_image;
private Spinner sp_file;
private String mPath;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_read);
iv_image = findViewById(R.id.iv_image);
sp_file = findViewById(R.id.sp_file);
findViewById(R.id.btn_delete).setOnClickListener(this);
// 获取当前App的私有存储目录
mPath = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).toString() + "/";
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
refreshSpinner();
} else {
showToast("未发现已挂载的SD卡,请检查");
}
}
private void refreshSpinner() {
// 获得指定目录下面的所有图片文件
ArrayList<File> fileAlllist = FileUtil.getFileList(mPath, new String[]{".png", ".jpg"});
if (fileAlllist.size() > 0) {
fileArray = new String[fileAlllist.size()];
for (int i = 0; i < fileAlllist.size(); i++) {
fileArray[i] = fileAlllist.get(i).getName();
}
ArrayAdapter<String> typeAdapter = new ArrayAdapter<String>(this,
R.layout.item_select, fileArray);
typeAdapter.setDropDownViewResource(R.layout.item_dropdown);
sp_file.setPrompt("请选择图片文件");
sp_file.setAdapter(typeAdapter);
sp_file.setSelection(0);
sp_file.setOnItemSelectedListener(new FileSelectedListener());
} else {
fileArray = null;
fileArray = new String[1];
fileArray[0] = "";
ArrayAdapter<String> typeAdapter = new ArrayAdapter<String>(this,
R.layout.item_select, fileArray);
sp_file.setPrompt(null);
sp_file.setAdapter(typeAdapter);
sp_file.setOnItemSelectedListener(null);
iv_image.setImageDrawable(null);
}
}
private String[] fileArray;
class FileSelectedListener implements AdapterView.OnItemSelectedListener {
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// 打开并显示选中的图片文件位图
String file_path = mPath + fileArray[arg2];
Bitmap bitmap = FileUtil.openImage(file_path);
iv_image.setImageBitmap(bitmap);
}
public void onNothingSelected(AdapterView<?> arg0) {
}
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.btn_delete) {
for (int i = 0; i < fileArray.length; i++) {
String file_path = mPath + fileArray[i];
File f = new File(file_path);
if (!f.delete()) {
Log.d(TAG, "file_path=" + file_path + ", delete failed");
}
}
refreshSpinner();
showToast("已删除临时目录下的所有图片文件");
}
}
private void showToast(String desc) {
Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
}
}