BitmapFactory.decodeFile() returns null - android

I am creating an Android app in which you copy an image from location x to location y. After the copying is complete I would like to see the picture in a ImageView.
I know the images location, but no matter what I try I can't create a bitmap object of it.
The line which are causing my problems is this:
BitmapFactory.decodeFile(dir+s);
dir = getCacheDir().getAbsolutePath()+"/images/";
s = file name (eg. 1275123123.jpg)
If I create a File object with the same path, and call f.isFile(), it returns true.
Opening the image in either android or windows are not a problem.

Didn't you forget to add SD card access permissions READ_EXTERNAL_STORAGE and/or WRITE_EXTERNAL_STORAGE ?

I had a similar problem and what eventually solved it was creating FileInputStream(...) and decoding the stream. This is the code I used, I changed it to suite your case:
File location = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+ "/images");
File dest = new File(location, fileName + ".JPG");
FileInputStream fis;
fis = new FileInputStream(dest);
Bitmap img = BitmapFactory.decodeStream(fis);

You have to add
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
permissions to you manifest.
And please don't forget to request runtime permission from user.

Related

Cannot create directory and save picture taken by camera in storage/emulated/0

First and foremost, I'm testing this app using my Google Pixel XL (1st gen 32 GB version, runnning Android 9 API Level 28)
Basically, I'm trying to make a camera application based on Android Camera 2 API (I just downloaded the sample code from https://github.com/googlesamples/android-Camera2Basic). and I'm stuck because I'm trying to save the picture taken by the camera into "Pictures" folder inside "/storage/emulated/0/".
However, for some reason even though it seems like I'm able to do this, the folder and the picture file are not there when I checked them with ES File Explorer.
I've tried 3 methods so far:
Attempt #1:
mFile = new File(getActivity().getExternalFilesDir(Environment.DIRECTORY_PICTURES), "newPicture.jpg");
if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){
mFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "picture.jpg");
mFile.mkdirs();
}
Attempt #2:
File filePath = new File(Environment.getExternalStorageDirectory().getPath(),"randomFolder");
if(!filePath.exists()){
filePath.mkdirs();
}
String fullPathName = filePath.getAbsolutePath() + "/" + "newPicture.jpg";
mFile = new File(fullPathName);
Attempt #3:
mFile = new File(fullPathName);
File filePath = new File(Environment.getExternalStorageDirectory() + "/Pictures/");
if (!filePath.exists()){
filePath.mkdirs();
}
mFile = new File(filePath,"newPicture.jpg");
mFile is the File path where "newPicture" will be saved to, basically it's supposed to be saved to "/storage/emulated/0/Pictures/newPicture.jpg"
Now, I've tried to use getExternalFilesDir() method so the picture will be saved into /storage/emulated/0/Android/data/com.***.***[the package name]/files/Pictures/newPicture.jpg" and in this case, IT TOTALLY WORKS! However I REALLY want to save the picture to /storage/emulated/0 directory because I want user to be able to access the data easily later on.
In order to illustrate this better, I've recorded the application running and then took a picture and saved it in a gif, uploaded it to imgur right here:
https://imgur.com/Szpc1HR
See that? The picture seems to be saved into "/storage/emulated/0/Pictures/newPicture.jpg" successfully but then when I went to ES File Explorer to check the picture, the folder "Pictures" was not created, nada.
I'm literally on my wits end right here haha, does anyone know a solution to this problem? Any advice will be greatly appreciated
P.S. Almost forgot to say, I've already added the permission in AndroidManifest.xml:
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
As someone said in this post:
how-can-i-access-storage-emulated-0-dcim-on-android-device
,
The "/storage/emulated/" folder does not really exist. It's what might be called a "symbolic link", or, in simpler terms, a reference to where the real data is stored. You'll need to find the actual physical location on your device where it is stored.
Maybe this link will help, Or this Can't create a directory on /storage/emulated/0 on emulator.

Trying to save a file on an android system and make it downloadable by the user

I am sorry I am extremely new to android and I am lost. I successfully found out how to save a file on an android system, but every time I try to search for the file on my Android phone (which is where I have a copy of this app located) I am unable to locate it. I know it is there because the app would not start up with out it. How do you write a file that can be both used by the App AND searched by the user in the File Explorer. I am using a Galaxy Note 3 Android version 5.0 Any help would be appreciated.
private void WriteFile(String FileName, String FileCont, boolean isAppend){
File file = new File(getApplicationContext().getFilesDir().getAbsolutePath() + "/" + FileName);
try {
FileOutputStream stream = new FileOutputStream(file, isAppend);
stream.write(FileCont.getBytes());
stream.close();
}
catch(Exception ex){
}
}
Did set the permissions to write on external space in your manifest.xml?
For reference see
http://developer.android.com/reference/android/Manifest.permission.html
You have to set the "WRITE_EXTERNAL_STORAGE" permission to write on external disk. Just add the following line to your android manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
It will implicitly contain the permission to read external storage as well ;).

Android Permissions Issue

I am writing a simple activity to record and save audio, preferably to a folder within my application, but, for simplicity, to the SD card. The line of code that's giving me trouble is
String path = Environment.getExternalStorageDirectory().toString() + "/" + "tempAppFiles/";
String filename = "test"+".mp4";
recorder.setOutputFile(path + filename);
where recorder is an instance of MediaRecorder.
When I run the application, I get a permissions error that states
07-31 15:51:51.810: W/System.err(13670): java.io.FileNotFoundException: /mnt/sdcard/tempAppFiles/test.mp4 (Permission denied)
I looked this problem up and found that I needed to add several permission tags to my manifest, and I added
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
to my manifest.
I am still getting the same permissions issue, and I can't find anyone with a similar problem.
Any ideas?
Hmm... The permissions seem to be correct, perhaps you've put them in the wrong place, they need to be children of the root note .
Next you can check, whether you can write the file by checking
boolean canIWrite = path.canWrite();
As you also get a FileNotFound exception you could try...
String path = Environment.getExternalStorageDirectory().getAbsolutePath();
...instead of...
String path = Environment.getExternalStorageDirectory().toString();
If that is no help at all, there is still an official example of capturing audio - you should compare your code to:
http://developer.android.com/guide/topics/media/audio-capture.html
tempAppFiles does not set well with me. Seems like you should be writing to a directory that is under the package name of the app you are writing ...

Android: Save data to internal memory using Download

I am downloading file from a server in Android using the DownloadManager class. I want to store this file in the internal memory of the device. I tried to use .setDestinationInExternalPublicDir(Environment.getExternalStorageDirectory() +"/Android/data/xxx.xxx.xxx/files/") as mentioned here, but it is not working. How to solve my problem?
add
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
to manifest.xml and create "/Android/data/xxx.xxx.xxx/files/" path
try this
File testDirectory = new File(Environment.getExternalStorageDirectory() +"/Android/data/xxx.xxx.xxx/files/");
and
if (!testDirectory.exists()) {
testDirectory.mkdirs();
}

Permission denied: File created in .../files

I'm creating a file in data/data/myPackage/files/ :
file = new File( getFilesDir() + "/file.txt");
I'm absolutely sure that the file is created.
Right after its creation I call:
file.canWrite();
and the result is true.
When I try to use that file
I get: "Permission Denied".
In Eclipse, in DDMS, this file permissions are like:
-rw-------
Can anyone help here?
Thanks
Ensure you have the correct permissions to write to the file. In your manifest file, include this line
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />.
Easiest way to open the file (and create it at the same time) would be to use the openFileOutput("file.txt", MODE_PRIVATE) method.
This ensures that the file is only readable by your application (the same as you've have at the moment), plus it returns you a FileOutputStream so you can start writing to it.
The permissions look correct to me. Each application runs as a different user so only has access to it's own files.
When you say "I try to use that file" what do you mean? Can you post the code for that as I think that is where the problem lies.
If you know the file is being created using File() and you don't want to use openFileOutput() then try this:
FileWriter fWriter;
try {
fWriter = new FileWriter(file, true);
fWriter.write("hello");
fWriter.close();
} catch (IOException e) {
// log error
}
I have been working on the problem of nullpointerexception, file not found, for a few hours. I have moved from the emulator to an actual device. In the emulator the following worked:
BufferedWriter osw = new BufferedWriter(new FileWriter(path));
When I went directly to the device though, I got the exception.
Neelesh Gajender has hit the answer perfectly. Adding: :
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
in the android manifest solves this problem completely. I am grateful to Neelesh and Kudos!
I've found that for the writing on '/data/data/myPackage/files/',
permissions need to be set.
There was MODE_WORLD_WRITEABLE,
that was the one with interest for this case,
but even with this permission set, I still got error.
I thought: "maybe what I need is an EXECUTE permission...".
There is no such permission in the API
but even though I tried to put a 3 in the mode parameter
(MODE_WORLD_WRITEABLE is = 2),
and...it worked!
But when I write to the sd card with that permission set,
I get an error as well,
so I write differently to the two folders.
int permission = 3;
if (whereToWrite == WRITE_TO_DATA_FOLDER) {
FileOutputStream fos = openFileOutput(file.getName(), permission);
buffOutStream = new BufferedOutputStream(fos);
} else if (whereToWrite == WRITE_TO_SD_CARD){
buffOutStream = new BufferedOutputStream(new FileOutputStream(file));
}
And by the way,
MODE_WORLD_WRITEABLE is wrong,
MODE_WORLD_WRITABLE is right.

Categories

Resources