我目前正在使用此代码来存储图像(可能是错误的)。
路径是这样的:
public static final String IMAGE_DIR = "test";
保存图片的代码如下:
public class ImageSaver {
private String directoryName = Constants.IMAGE_DIR;
private String fileName = "";
private Context context;
private boolean external;
public ImageSaver(Context context) {
this.context = context;
}
public ImageSaver setFileName(String fileName) {
this.fileName = fileName;
return this;
}
public ImageSaver setExternal(boolean external) {
this.external = external;
return this;
}
public ImageSaver setDirectoryName(String directoryName) {
this.directoryName = directoryName;
return this;
}
public int save(Bitmap bitmapImage, int jpgOrPng) {
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(createFile());
if(jpgOrPng == 0) {
bitmapImage.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
} else {
bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
}
Log.d("THE PICTURE ", " The picture finished saving");
} catch (Exception e) {
e.printStackTrace();
return 0;
} finally {
try {
if (fileOutputStream != null) {
fileOutputStream.close();
}
return 1;
} catch (IOException e) {
e.printStackTrace();
return 0;
}
}
}
@NonNull
private File createFile() {
File directory;
if(external){
directory = getAlbumStorageDir(directoryName);
}
else {
directory = context.getDir(directoryName, Context.MODE_PRIVATE);
}
if(!directory.exists() && !directory.mkdirs()){
Log.e("ImageSaver","Error creating directory " + directory);
}
return new File(directory, fileName);
}
private File getAlbumStorageDir(String albumName) {
return new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), albumName);
}
public static boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
return Environment.MEDIA_MOUNTED.equals(state);
}
public static boolean isExternalStorageReadable() {
String state = Environment.getExternalStorageState();
return Environment.MEDIA_MOUNTED.equals(state) ||
Environment.MEDIA_MOUNTED_READ_ONLY.equals(state);
}
public Bitmap load() {
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(createFile());
return BitmapFactory.decodeStream(inputStream);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
public boolean deleteFile(){ File file = createFile(); return file.delete(); }
}
我在主页上这样称呼它:
Bitmap foodImage = getImageBitmapFromURL(this, Constants.FOOD_IMAGE + menuItemReponseList.get(storeItemCounter).getPicture());
String pictureToCheck = menuItems.get(0).getPicture();
String[] words = pictureToCheck.split(Pattern.quote("."));
String newPic = words[0];
int jpgOrPng = 0;
if(newPic.equals("png")) {
jpgOrPng = 1;
Log.d("THE PICTURE IS ", "Poc png: " + newPic);
}
int done = new ImageSaver(this).
setFileName(menuItems.get(0).getPicture()).
setDirectoryName(Constants.IMAGE_DIR).
save(foodImage, jpgOrPng);
这是 getBitmap 方法:
public static Bitmap getImageBitmapFromURL(final Context context, final String imageUrl){
Bitmap imageBitmap = null;
try {
imageBitmap = new AsyncTask<Void, Void, Bitmap>() {
@Override
protected Bitmap doInBackground(Void... params) {
try {
int targetHeight = 200;
int targetWidth = 300;
return Picasso.get().load(String.valueOf(imageUrl))
.resize(targetWidth, targetHeight)
.placeholder(R.drawable.burger)
.error(R.drawable.burger)
.get();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}.execute().get();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return imageBitmap;
}
这段代码的重点是使图像可以离线使用,因此它们可以从互联网上下载一次,然后指向并加载,如下所示:
Bitmap bitmap = new ImageSaver(this).
setFileName(newPic).
setDirectoryName(Constants.IMAGE_DIR).
load();
homeSuggestPic.setImageBitmap(bitmap);
我试图找到它存储的目录,以确保它实际上存储在设备上。不幸的是,当使用文件管理器时,我的应用程序名称甚至不会出现在数据或任何地方。想一想,我是不是存储不正确?