嘿我想在外部存储器中保存图像。我有两个版本,但两个都没有用。目标是用户单击按钮并保存图像,然后用户也可以在库中看到它。所以这是第一版:
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();
}
感谢您的帮助
答案 0 :(得分:1)
试试这个
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,则始终使用AsyncTask。
答案 1 :(得分:-1)
较晚但可能会有所帮助
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);
}
}