我需要将使用我的应用程序拍摄的照片保存在特定文件夹中.我已经阅读了很多这个问题的解决方案,但我无法使它们中的任何一个工作,所以我寻求帮助. MainActivity.java public void onClick(Vi
MainActivity.java
public void onClick(View v) { Intent camera = new Intent( android.provider.MediaStore.ACTION_IMAGE_CAPTURE); //Folder is already created String dirName = Environment.getExternalStorageDirectory().getPath() + "/MyAppFolder/MyApp" + n + ".png"; Uri uriSavedImage = Uri.fromFile(new File(dirName)); camera.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage); startActivityForResult(camera, 1); n++; }
AndroidManifest.xml中
<uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />通过以下代码,它对我来说很好.
private void createDirectoryAndSaveFile(Bitmap imageToSave, String fileName) { File direct = new File(Environment.getExternalStorageDirectory() + "/DirName"); if (!direct.exists()) { File wallpaperDirectory = new File("/sdcard/DirName/"); wallpaperDirectory.mkdirs(); } File file = new File(new File("/sdcard/DirName/"), fileName); if (file.exists()) { file.delete(); } try { FileOutputStream out = new FileOutputStream(file); imageToSave.compress(Bitmap.CompressFormat.JPEG, 100, out); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); } }