android 如何将位图保存到应用程序文件夹
How to Save bitmap to app folder in android
我有一张位图,我想将它保存到应用程序文件夹中。我尝试使用这些代码:
ContextWrapper contextWrapper = new ContextWrapper(context.getApplicationContext());
File directory = contextWrapper.getDir("tabs", Context.MODE_PRIVATE);
if (!directory.exists())
directory.mkdir();
String fname = "Image.jpg";
File file = new File(directory, fname);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 50, fos);
fos.close();
} catch (Exception e) {
Log.e("SAVE_IMAGE", e.getMessage(), e);
}
现在,我有两个问题。
- 为什么显示此警告以及如何解决?
Result of 'File.mkdir()' is ignored
- 目录"app_tabs"在应用程序文件夹中创建,但未保存位图且文件夹中没有照片。如何保存此位图?
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString() + "/tabs";
File dir = new File(path);
if (!dir.exists())
dir.mkdirs();
File futureStudioIconFile = new File(path, "Image.jpg);
if (futureStudioIconFile.exists())
futureStudioIconFile.delete();
futureStudioIconFile.createNewFile();
试试这个,希望对你有帮助
你可以这样做:
try {
FileOutputStream fileOutputStream = context.openFileOutput("Your File Name", Context.MODE_PRIVATE);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
fileOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
它将您的位图保存在 app 文件夹的 "files" 目录中。
技术上你的代码是正确的,而且我在我自己的设备上试过都是正确的。
所以我怀疑您找到了其他路径,或者如果您使用 Android Studio 检查文件,则需要单击 'Synchronize' 菜单。
顺便说一句,你可以直接使用上下文或者context.getApplicationContext()来调用getDir(),不需要新建一个ContextWrapper。像这样:
File directory = context.getDir("tabs", Context.MODE_PRIVATE);
这是我的回答
Result of 'File.mkdir()' is ignored
:因为 mkdir() return 一个布尔值,true
如果文件夹已创建,否则为 false。
我是这样保存位图的
File storageDir = getFilesDir();
File filePath = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
try (FileOutputStream out = new FileOutputStream(filePath)) {
capturedImg.compress(Bitmap.CompressFormat.JPEG, quality, out);
} catch (IOException e) {
e.printStackTrace();
}
或者如果您只想以原始分辨率保存位图
public static void copy(Uri uri, File dst, Context context) {
try (InputStream in = context.getContentResolver().openInputStream(uri);
OutputStream out = new FileOutputStream(dst)) {
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
}
}
我有一张位图,我想将它保存到应用程序文件夹中。我尝试使用这些代码:
ContextWrapper contextWrapper = new ContextWrapper(context.getApplicationContext());
File directory = contextWrapper.getDir("tabs", Context.MODE_PRIVATE);
if (!directory.exists())
directory.mkdir();
String fname = "Image.jpg";
File file = new File(directory, fname);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 50, fos);
fos.close();
} catch (Exception e) {
Log.e("SAVE_IMAGE", e.getMessage(), e);
}
现在,我有两个问题。
- 为什么显示此警告以及如何解决?
Result of 'File.mkdir()' is ignored
- 目录"app_tabs"在应用程序文件夹中创建,但未保存位图且文件夹中没有照片。如何保存此位图?
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString() + "/tabs";
File dir = new File(path);
if (!dir.exists())
dir.mkdirs();
File futureStudioIconFile = new File(path, "Image.jpg);
if (futureStudioIconFile.exists())
futureStudioIconFile.delete();
futureStudioIconFile.createNewFile();
试试这个,希望对你有帮助
你可以这样做:
try {
FileOutputStream fileOutputStream = context.openFileOutput("Your File Name", Context.MODE_PRIVATE);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
fileOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
它将您的位图保存在 app 文件夹的 "files" 目录中。
技术上你的代码是正确的,而且我在我自己的设备上试过都是正确的。
所以我怀疑您找到了其他路径,或者如果您使用 Android Studio 检查文件,则需要单击 'Synchronize' 菜单。
顺便说一句,你可以直接使用上下文或者context.getApplicationContext()来调用getDir(),不需要新建一个ContextWrapper。像这样:
File directory = context.getDir("tabs", Context.MODE_PRIVATE);
这是我的回答
Result of 'File.mkdir()' is ignored
:因为 mkdir() return 一个布尔值,true
如果文件夹已创建,否则为 false。我是这样保存位图的
File storageDir = getFilesDir();
File filePath = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
try (FileOutputStream out = new FileOutputStream(filePath)) {
capturedImg.compress(Bitmap.CompressFormat.JPEG, quality, out);
} catch (IOException e) {
e.printStackTrace();
}
或者如果您只想以原始分辨率保存位图
public static void copy(Uri uri, File dst, Context context) {
try (InputStream in = context.getContentResolver().openInputStream(uri);
OutputStream out = new FileOutputStream(dst)) {
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
}
}