将Android中的位图保存为外部存储中的JPEG文件夹

不操千曲而后晓声,观千剑而后识器。这篇文章主要讲述将Android中的位图保存为外部存储中的JPEG文件夹相关的知识,希望能为你提供帮助。
我使用此代码将位图保存在外部存储中但如果它不存在则不会创建该文件夹:

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())));

看看这个链接以获得明确答案:show folder images in gallery
另一答案请使用下面的代码片段可能会有所帮助
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; }

另一答案【将Android中的位图保存为外部存储中的JPEG文件夹】您应该查看File的文档,您将找到方法mkdir()。它与unix one几乎相同:https://developer.android.com/reference/java/io/File.html#mkdir()

    推荐阅读