Reputation: 84
I want to save a picture file from Internet to my mobile. I have used both Internet and externalstorage Permission in AndroidManifest file. But it gives an error given below. I think it is because my device don't have an SD CARD. If so then I want to know how to store in Internal storage.
myError
java.io.FileNotFoundException: /storage/emulated/0/Pictures/GettyImages-460712009-560x450.jpg: open failed: EACCES (Permission denied Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)
Below is my code.
try {
URL urlobj = new URL(url);
connection= (HttpURLConnection) urlobj.openConnection();
inputStream=connection.getInputStream();
file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath() + "/"+ Uri.parse(url).getLastPathSegment());
FileOutputStream fileOutputStream = new FileOutputStream(file);
int reed = -1;
byte[] buffer = new byte[1024];
// if no data is return -1 keep reading until -1
while( ( reed=inputStream.read(buffer) )!= -1){
fileOutputStream.write(buffer,0,reed);
}
Upvotes: 0
Views: 217
Reputation: 2905
To save an image in internal memory use this code.
You should add permission in you manifast file<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
You need run time permission in marshmallow and above version, for that use Amit sharma Answer.
Here is the code to save a file in Default picture directory.
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
For mode details please check this Link
Upvotes: 0
Reputation: 645
You are getting Permission denied Exception because you are not implementing Run Time permission for Devices having Android Marshmallow and above version ..
This is how you can achieve this :
Add following permission to manifest like earlier :
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Match if the user has already granted the permission. If yes, skip asking for permission and continue with your work else ask user for permission :
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!Settings.System.canWrite(this)) {
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE}, 2909);
} else {
// your code
}
} else {
// your code
}
Permission result callback:
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case 2909: {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.e("Permission", "Granted");
} else {
Log.e("Permission", "Denied");
}
return;
}
}
}
Upvotes: 0