将图像保存在外部存储器上
Save image on external Storage
嘿,我想在外部存储中保存图像。我从这里得到了两个版本,但都不起作用。目标是用户单击一个按钮并保存图像,然后用户也可以在图库中看到它。所以这里是版本一:
String path = Environment.getExternalStorageDirectory().getPath();
File outputDir= new File(path);
outputDir.mkdirs();
File newFile = new File(path+"/"+"test.png");
FileOutputStream out = null;
try {
out = new FileOutputStream(newFile);
mutableBitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
这是版本 2:
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + File.separator + "MyApplication";
File outputDir= new File(path);
outputDir.mkdirs();
File newFile = new File(path+"/"+"test.png");
FileOutputStream out = null;
try {
out = new FileOutputStream(newFile);
mutableBitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
感谢帮助
试试这个
AsyncTask fileTask = new AsyncTask() {
@Override
protected Object doInBackground(Object[] objects) {
File directory = new File(Environment.getExternalStorageDirectory() + File.separator + "MyApplication");
if (!directory.exists()) {
directory.mkdirs();
}
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String name = " "+n+".jpg";
File pictureFile = new File(directory, name);
pictureFile.createNewFile();
try {
FileOutputStream out = new FileOutputStream(pictureFile);
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
};
fileTask.execute();
如果所有文件处理权限都可用于您的应用程序,这应该可以正常工作。
另请注意,永远不要在主线程中执行文件操作,因此如果您还没有这样做,请始终使用 AsyncTask。
晚了但可能会有帮助
private void saveimage(Bitmap img) {
try {
String imageName = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss").format(new Date());
boolean imageSaved = false;
if (!(img == null || img.isRecycled())) {
ContentResolver contentResolver = getContentResolver();
File file = new File(getStoragePath() + "img path/");
if (!file.exists()) {
file.mkdirs();
}
File imageFile = new File(file, String.format("%s.png", new Object[]{imageName}));
try {
FileOutputStream out = new FileOutputStream(imageFile);
imageSaved = img.compress(CompressFormat.PNG, 100, out);
if (out != null) {
out.flush();
out.close();
}
} catch (Exception e) {
Log.e(TAG, "Unable to write the image to gallery", e);
}
ContentValues values = new ContentValues(8);
values.put(SettingsJsonConstants.PROMPT_TITLE_KEY, imageName);
values.put("_display_name", camera.name);
String location = "";
if (camera.country != null && camera.country.length() > 0) {
location = camera.country;
}
if (camera.city != null && camera.city.length() > 0) {
location = location + ", " + camera.city;
}
values.put("description", location);
values.put("mime_type", "image/png");
values.put("description", "");
long millis = System.currentTimeMillis();
values.put("date_added", Long.valueOf(millis / 1000));
values.put("datetaken", Long.valueOf(millis));
values.put("_data", imageFile.getAbsolutePath());
contentResolver.insert(Media.EXTERNAL_CONTENT_URI, values);
MediaScannerConnection.scanFile(getApplicationContext(), new String[]{imageFile.getPath()}, new String[]{"image/png"}, new OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
}
});
}
if (imageSaved) {
}
} catch (Exception e2) {
Log.e(TAG, "saveSnapshot", e2);
}
}
嘿,我想在外部存储中保存图像。我从这里得到了两个版本,但都不起作用。目标是用户单击一个按钮并保存图像,然后用户也可以在图库中看到它。所以这里是版本一:
String path = Environment.getExternalStorageDirectory().getPath();
File outputDir= new File(path);
outputDir.mkdirs();
File newFile = new File(path+"/"+"test.png");
FileOutputStream out = null;
try {
out = new FileOutputStream(newFile);
mutableBitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
这是版本 2:
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + File.separator + "MyApplication";
File outputDir= new File(path);
outputDir.mkdirs();
File newFile = new File(path+"/"+"test.png");
FileOutputStream out = null;
try {
out = new FileOutputStream(newFile);
mutableBitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
感谢帮助
试试这个
AsyncTask fileTask = new AsyncTask() {
@Override
protected Object doInBackground(Object[] objects) {
File directory = new File(Environment.getExternalStorageDirectory() + File.separator + "MyApplication");
if (!directory.exists()) {
directory.mkdirs();
}
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String name = " "+n+".jpg";
File pictureFile = new File(directory, name);
pictureFile.createNewFile();
try {
FileOutputStream out = new FileOutputStream(pictureFile);
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
};
fileTask.execute();
如果所有文件处理权限都可用于您的应用程序,这应该可以正常工作。
另请注意,永远不要在主线程中执行文件操作,因此如果您还没有这样做,请始终使用 AsyncTask。
晚了但可能会有帮助
private void saveimage(Bitmap img) {
try {
String imageName = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss").format(new Date());
boolean imageSaved = false;
if (!(img == null || img.isRecycled())) {
ContentResolver contentResolver = getContentResolver();
File file = new File(getStoragePath() + "img path/");
if (!file.exists()) {
file.mkdirs();
}
File imageFile = new File(file, String.format("%s.png", new Object[]{imageName}));
try {
FileOutputStream out = new FileOutputStream(imageFile);
imageSaved = img.compress(CompressFormat.PNG, 100, out);
if (out != null) {
out.flush();
out.close();
}
} catch (Exception e) {
Log.e(TAG, "Unable to write the image to gallery", e);
}
ContentValues values = new ContentValues(8);
values.put(SettingsJsonConstants.PROMPT_TITLE_KEY, imageName);
values.put("_display_name", camera.name);
String location = "";
if (camera.country != null && camera.country.length() > 0) {
location = camera.country;
}
if (camera.city != null && camera.city.length() > 0) {
location = location + ", " + camera.city;
}
values.put("description", location);
values.put("mime_type", "image/png");
values.put("description", "");
long millis = System.currentTimeMillis();
values.put("date_added", Long.valueOf(millis / 1000));
values.put("datetaken", Long.valueOf(millis));
values.put("_data", imageFile.getAbsolutePath());
contentResolver.insert(Media.EXTERNAL_CONTENT_URI, values);
MediaScannerConnection.scanFile(getApplicationContext(), new String[]{imageFile.getPath()}, new String[]{"image/png"}, new OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
}
});
}
if (imageSaved) {
}
} catch (Exception e2) {
Log.e(TAG, "saveSnapshot", e2);
}
}