我在Uri中获取了我的图像和视频。我想将该文件保存在内部存储器的特定文件夹中。我尝试了Fileoutputstream,但我可以将其保存在相机文件夹中,但不能保存在特定的文件夹中。我想保存图像和视频。
public void onClick(View view) {
if (string.contains("jpg")) {
String path = null;
try {
path = MediaStore.Images.Media.insertImage(getContentResolver(),
string, "img", null);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
final Uri uri = Uri.parse(path);
Toast.makeText(getApplicationContext(), "Image saved", Toast.LENGTH_LONG).show();
saveImageandVideo(uri);
} else {
Uri uri = Uri.parse(string);
saveImage(uri);
Toast.makeText(getApplicationContext(), "Video saved", Toast.LENGTH_LONG).show();
}
}
});
我的保存代码
private void saveImageandVideo(Uri uri) {
String root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();
File myDir = new File(root + "/folder_name");
if (!myDir.exists()) {
myDir.mkdirs();
}
try {
FileOutputStream out = new FileOutputStream(file);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
此代码有效,但我无法将其保存在特定文件夹中。它会自动将其保存在相机文件夹(DCIM)中。我无法保存视频。等待答案的朋友。提前致谢:D
答案 0 :(得分:0)
按如下方式更改
file
变量创建。
String root = Environment.getExternalStorageDirectory().getAbsolutePath().toString();
File myDir = new File(root + "/folder_name");
if (!myDir.exists()) {
myDir.mkdirs();
}
File file = new File(myDir,"filename.jpg/mp4");
//rest of your code
将saveImageandVideo()更改为
private void saveImageandVideo() {
try {
FileOutputStream out = new FileOutputStream(file);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}