hello i\'m trying to save pictures taken on my application, but when i try to access the memory to place the data, an error comes out
unable to decode stream java.io
I had the same error, and the only problem was the permission in android manifest.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
You need to write to external storage, make sure you added the permission:
<manifest ...>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
...
</manifest>
Checks if external storage is available for read and write:
public boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
return true;
}
return false;
}
Use the root of the public directory instead of using the root of Android.
If you want to save public files on the external storage, use the getExternalStoragePublicDirectory()
public File getAlbumStorageDir(String albumName) {
// Get the directory for the user's public pictures directory.
File file = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DCIM), albumName);
if (!file.mkdirs()) {
Log.e(LOG_TAG, "Directory not created");
}
return file;
}
If you want to save files that are private to your app, use the getExternalFilesDir()
public File getAlbumStorageDir(Context context, String albumName) {
// Get the directory for the app's private pictures directory.
File file = new File(context.getExternalFilesDir(
Environment.DIRECTORY_DCIM), albumName);
if (!file.mkdirs()) {
Log.e(LOG_TAG, "Directory not created");
}
return file;
}
More information on the link http://developer.android.com/training/basics/data-storage/files.html
First, you are writing the image to a different location than you are reading it from. Rather than rebuild path
, use the tmpFile
value that you already have.
Second, do not use getRootDirectory()
to get the DCIM
directory. Use getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)
.
Third, use Log
methods to log exceptions, rather than just display a Toast
, as you may miss the Toast
and you do not get the stack trace associated with your exception.
I solved using this:
private static final int WRITE_PERMISSION = 0x01;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
requestWritePermission();
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
if(requestCode == WRITE_PERMISSION){
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.d(LOG_TAG, "Write Permission Failed");
Toast.makeText(this, "You must allow permission write external storage to your mobile device.", Toast.LENGTH_SHORT).show();
finish();
}
}
}
private void requestWritePermission(){
if(checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)!=PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},WRITE_PERMISSION);
}
}
Add this permission in Android Manifest.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
If it fails with this permission is present already, then you need to check your app's target SDK level. If it is targetSdkVersion => 23, then you need to request permissions at Run Time. Here