[英]Save Bitmap in Android as JPEG in External Storage in a folder
我正在使用此代碼將位圖保存在外部存儲中,但如果該文件夾不存在,則不會創建該文件夾:
String path = Environment.getExternalStorageDirectory().toString();
OutputStream fOutputStream = null;
File file = new File(path + "/Captures/", "screen.jpg");
try {
fOutputStream = new FileOutputStream(file);
capturedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOutputStream);
fOutputStream.flush();
fOutputStream.close();
MediaStore.Images.Media.insertImage(getContentResolver(), file.getAbsolutePath(), file.getName(), file.getName());
} catch (FileNotFoundException e) {
e.printStackTrace();
Toast.makeText(this, "Save Failed", Toast.LENGTH_SHORT).show();
return;
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this, "Save Failed", Toast.LENGTH_SHORT).show();
return;
}
如果圖像不存在,如何將圖像保存在新目錄中,如果文件夾在設備中,如何保存默認值?
試試這個它會給你結果肯定:
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/req_images");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-" + n + ".jpg";
File file = new File(myDir, fname);
Log.i(TAG, "" + file);
if (file.exists())
file.delete();
try {
FileOutputStream out = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
添加這個以在畫廊中顯示:
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
查看此鏈接以獲得明確的答案:在畫廊中顯示文件夾圖像
請使用下面的代碼片段可能會有所幫助
String path = Environment.getExternalStorageDirectory().toString();
OutputStream fOutputStream = null;
File file = new File(path + "/Captures/", "screen.jpg");
if (!file.exists()) {
file.mkdirs();
}
try {
fOutputStream = new FileOutputStream(file);
capturedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOutputStream);
fOutputStream.flush();
fOutputStream.close();
MediaStore.Images.Media.insertImage(getContentResolver(), file.getAbsolutePath(), file.getName(), file.getName());
} catch (FileNotFoundException e) {
e.printStackTrace();
Toast.makeText(this, "Save Failed", Toast.LENGTH_SHORT).show();
return;
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this, "Save Failed", Toast.LENGTH_SHORT).show();
return;
}
使用以下內容:
File dir = new File(path + "/Captures/");
if(!dir.exists()) {
dir.mkdirs();
}
File file = new File(path + "/Captures/", "screen.jpg");
......
遲到但可能對某人有幫助。 使用下面的代碼,由於 BufferOutPutStream,它將更快地將位圖保存在外部目錄中。
public boolean storeImage(Bitmap imageData, String filename) {
// get path to external storage (SD card)
File sdIconStorageDir = null;
sdIconStorageDir = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/myAppDir/");
// create storage directories, if they don't exist
if (!sdIconStorageDir.exist()) {
sdIconStorageDir.mkdirs();
}
try {
String filePath = sdIconStorageDir.toString() + File.separator + filename;
FileOutputStream fileOutputStream = new FileOutputStream(filePath);
BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
//Toast.makeText(m_cont, "Image Saved at----" + filePath, Toast.LENGTH_LONG).show();
// choose another format if PNG doesn't suit you
imageData.compress(Bitmap.CompressFormat.PNG, 100, bos);
bos.flush();
bos.close();
} catch (FileNotFoundException e) {
Log.w("TAG", "Error saving image file: " + e.getMessage());
return false;
} catch (IOException e) {
Log.w("TAG", "Error saving image file: " + e.getMessage());
return false;
}
return true;
}
您應該查看 File 的文檔,您會找到 mkdir() 方法。 它與 unix 幾乎相同: https : //developer.android.com/reference/java/io/File.html#mkdir()
這就是我在 Koltin 的案例中所做的:
fun getImageUriFromBitmap(context: Context, bitmap: Bitmap?): Uri {
var filePath = ""
try {
filePath = createAndGetFilePath(context)
val fileOutputStream = FileOutputStream(filePath)
val bos = BufferedOutputStream(fileOutputStream)
bitmap?.compress(Bitmap.CompressFormat.PNG, 100, bos)
bos.flush()
bos.close()
} catch (e: FileNotFoundException) {
Log.w("TAG", "Error saving image file: " + e.message)
} catch (e: IOException) {
Log.w("TAG", "Error saving image file: " + e.message)
}
return Uri.parse(filePath)
}
private fun createAndGetFilePath(context: Context): String {
val file = File(
context.getExternalFilesDir(Environment.DIRECTORY_PICTURES), // you can change the path here
BuildConfig.localImagesFolder // add it in build.gradle under android
)
try {
if (!file.exists()) {
file.mkdirs()
}
} catch (e: java.lang.Exception) {
e.printStackTrace()
}
return file.absolutePath + "/" + System.currentTimeMillis() + ".jpg"
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.