Please help me to solve this problem in my code ,I would like to save the bitmap image (newimage) into SD card in my phone (Galaxy j7), but I get the error (no such file or directory)
File filepath;
filepath = Environment.getExternalStorageDirectory();
File dir = new File(filepath.getAbsolutePath()+"/save image");
dir.mkdirs();
File file = new File(dir,"myimage.png");
Toast.makeText(MainActivity.this,"image saved to SD",Toast.LENGTH_LONG).show();
try{
OutputStream stream ;
stream = new FileOutputStream(file);
newimage.compress(Bitmap.CompressFormat.PNG,100,stream);
stream.flush();
stream.close();
}catch (IOException e)
{
e.printStackTrace();
}
// Parse the saved image path to uri
Uri savedImageURI = Uri.parse(filepath.getAbsolutePath());
// Display the saved image to ImageView
// imagev.setImageURI(savedImageURI);
// Display saved image uri to TextView
tv_saved.setText("Image saved in external storage.\n" + savedImageURI);
the error message is :
W/System.err: java.io.FileNotFoundException: /storage/emulated/0/save image/myimage.png (No such file or directory)
I added the permission of write in manifest.xml as follow:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="21" />
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="21"/>
I think there is no problem with your codes, the problem is the version of Android that you are compiling your project on. On higher versions of the Android you should implement runtime permission for storage permission.
Refer to the answer here which is going to solve your problem.
Edit: There is another problem with your implementation that I recognized after posting my answer. Before trying to save your image on the directory named save image you should create this folder by implementing
String folder_main = "save image"; File f = new File(Environment.getExternalStorageDirectory(), folder_main); if(!f.exists()) {f.mkdirs();}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.