Function not recreating Bitmap from location in memory - android

I have this code:
Bitmap readBitmapImage() {
String imageInSD = "/sdcard/mac/"+strURL;
BitmapFactory.Options bOptions = new BitmapFactory.Options();
bOptions.inTempStorage = new byte[16*1024];
bitmap = BitmapFactory.decodeFile(imageInSD,bOptions);
return bitmap;
}
The decodeFile function returns a null value, which is assigned to bitmap. Why doesn't decodeFile return the Bitmap image of the file saved on the SDCard?
I am working with AndroidStudio.

The problem is with your image path, i think it's not returning the absolute path. So try to use this for getting your imageInSD path like below
String imageInSD=new File(getFilesDir(), "test.png").getAbsolutePath();
Here test.png is your image name and make sure it is present in your filepath.
Don't forget to add these permissions in your manifest while dealing with the sdcard
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Related

Why don't I have access to the environment. DIRECTORY_PICTURES? (decodeFile returns null)

I'm trying to get an image stored in the external memory of my Android device
I get the file path in the following way:
public File getFilePath() {
File externalFilesDir = actGetImage.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
if (null == externalFilesDir) {return null;}
return new File(externalFilesDir,"IMG" + ".jpg");
}
In this way the path of the file is:
/storage/emulated/0/Android/data/net.sicoy.sicoybox/files/Pictures/IMG.jpg
To obtain the image I do the following:
Bitmap image = BitmapFactory.decodeFile(path);
Where path is:
private File myFile;
myFile = getFilePath();
path = myFile.getAbsolutePath();
But BitmapFactory.decodeFile (path) returns a value of null and the permissions are enabled:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Any suggestions?

Displaying from external storage?

I have an image saved in my Pictures folder, how to display it in a imageview?
like:
imageview.setimage("//Pictures//cat.jpg)
I know it's not a correct code, but I want to achieve something like this, hope someone can help, thanks!
you first generate a bitmap from file path and then put that bitmap to imageview
File image = new File(filePath);
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeFile(image.getAbsolutePath(),bmOptions);
bitmap = Bitmap.createScaledBitmap(bitmap,parent.getWidth(),parent.getHeight(),true);
imageView.setImageBitmap(bitmap);
Edit: Also add this permission in your manifest
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
You can set image like this from the sd card get path and create file variable and decode file using BitmapFactory set imageview image
String path = Environment.getExternalStorageState()+"/Pictures//cat.jpg";
File f = new File(path);
imageview.setImageBitmap(new BitmapFactory.decodeFile(f.getAbsolutePath()));
First you passing wrong file path.
for Correct File path do like this
Environment.getExternalStoragePublicDirectory (Environment.DIRECTORY_PICTURES).getAbsolutePath());
then create your URL like.
String filePath = Environment.getExternalStoragePublicDirectory (Environment.DIRECTORY_PICTURES).getAbsolutePath()) + "/cat.jpg";
Then use like this.
File image = new File(filePath);
imageView.setImageBitmap(new BitmapFactory.decodeFile(image.getAbsolutePath()));
Got it working with combination of the 2 posted codes, thanks for everyone, here's the working code:
Environment.getExternalStoragePublicDirectory (Environment.DIRECTORY_PICTURES).getAbsolutePath();
String filePath = Environment.getExternalStoragePublicDirectory (Environment.DIRECTORY_PICTURES) + "/picFolder/1.jpg";
File image = new File(filePath);
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeFile(image.getAbsolutePath(),bmOptions);
image1.setImageBitmap(bitmap);

Android. How to load the picture from memory knowing its full path?

I saved the picture on phone from application. I found a file manager it and I made sure that it really remained. Further I try to load on its full path it by BitmapFactory.decodeFile() method transferring a full path to the picture, a way to pictures at me such there, I will give an example from application:
/storage/emulated/0/Android/data/com .example.home.page/files/2015218161530.jpg
But me jumps out Exception, what decoding is impossible since the file isn't found, what for nonsense? Thanks in advance
You can use this method this will work for you
just pass path of images(where your image is store and object reference of you ImageView as a second argument)
1st argument Path of images want ot display
2nd argument object reference of `ImageView`
public static void ShowPicture(String filePath, ImageView pic) {
File f = new File(filePath);
FileInputStream is = null;
try {
is = new FileInputStream(f);
} catch (FileNotFoundException e) {
Log.d("error: ",String.format( "ShowPicture.java file[%s]Not Found",fileName));
return;
}
Bitmap = BitmapFactory.decodeStream(is, null, null);
pic.setImageBitmap(bm);
}
please also put this permission in manifest file
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
try below code that might help you.
File f = new File(PathToFiles +yourFileName);
if (f.exists()) {
Drawable d = Drawable.createFromPath(f.getPath());
imageview.setImageDrawable(d);
}

Problems reading from SDCARD with getExternalStorageDirectory(), java.io.FileNotFoundException: /mnt/sdcard/magazine/lowres/1.jpg

i'm using the standard method for reading files stored on the sdcard:
public static Bitmap loadImage( String imageName ){
File root = Environment.getExternalStorageDirectory();
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inPreferredConfig = Bitmap.Config.RGB_565;
Bitmap image = BitmapFactory.decodeFile(root+"/magazine/lowres/"+imageName+".jpg", opt);
return image;
}
The image is on that directory, but i'm getting this excepction:
java.io.FileNotFoundException: /mnt/sdcard/magazine/lowres/1.jpg (No such file or directory)
What i'm doing wrong?
SOLVED:
Samsung Galaxy Tab has a good amount of internal flash memory. So Environment.getExternalStorageDirectory() returns /mnt/sdcard/ but this is actually the internal storage. The real external storage is in /mnt/sdcard/external_sd/.

Android: decodeFile always returns null for file in internal storage

I have a file saved locally into the application's private storage. I have verified it exists, however whenever I call BitmapFactory.decodeFile it always returns null.
If I save the file as a resource and use ImageView.setImageResource, it always shows up fine.
What is the problem?
Here is the snippet:
filename = "test.png";
if (doesFileExist(filename))
Bitmap bMap = BitmapFactory.decodeFile(filename);
I've also tried:
Bitmap bMap = BitmapFactory.decodeFile(getFilesDir().getPath()
+ filename);
This question has been answered before such as here:
BitmapFactory.decodeFile returns null even image exists
This was exactly what I needed:
String fname=new File(getFilesDir(), "test.png").getAbsolutePath();
Instead of using BitmapFactory.decodeFile, try using InputStream:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
if(resultCode == RESULT_OK){
Uri selectedImage = imageReturnedIntent.getData();
InputStream imageStream = getContentResolver().openInputStream(selectedImage);
Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);
Folks, files stored in app resource should be referenced in special way. E.g. if file is located in assets and named as "myfile.png" it has to be referenced as:
String uriString="file:///android_asset/myfile.png";
Uri uri=Uri.parse(uriString);
BitmapFactory.decodeFile expects a file path without the scheme. I.e. without the file:// in the beginning.
If you're handling a Uri, don't just .toString() it, but instead call .getPath() on it, and pass that to the method.
After adding the required permissions (READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE)
You need to add this line to manifests:
android:requestLegacyExternalStorage="true"
Could you try fileList()? It
returns an array of strings naming the
private files associated with this
Context's application package.
For me I was getting image from locally saved URL something like "file:///storage/emulated/0/...." (I have used Phonegap plugin to capture image. Plugin was giving me image path, which I need to use in native code)
Here is the code snippet which worked for me.
String captured_image_info = "file:///storage/emulated/0/Android/data/com.testapp/cache/1493809796526.jpg"
Uri uri=Uri.parse(captured_image_info);
largeLog("uri", "" + uri);
InputStream imageStream = getContentResolver().openInputStream(uri);
Bitmap bm = BitmapFactory.decodeStream(imageStream);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object
byte[] decodedBytes = baos.toByteArray();
Bitmap img_captured_image = BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.length);
You need to setup run-time permissions for Manifest.permission.READ_EXTERNAL_STORAGE
If you don't you'll get a null without getting any error message or even an indication in the Log.
Note that you need to request the permissions both in the Manifest and at run time.

Categories

Resources