Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting ImageView image using path of file from external storage

Tags:

android

Hi I wanted to change the image of my ImageView using a path I have saved on my SQLite database. Well what I want to achieve is for this runs per second whenever the image is available it will show and stay until a next image will be available. But will use a default image whenever there is nothing to show at all. But for now the main problem I wanted to solve is that I can't set the imageView using the path I have. I tried different solutions such as making a bitmap out from the path but still receiving the same error which is: E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /mnt/sdcard/Echo/Images/Awesome4 - 00:01.jpg: open failed: ENOENT (No such file or directory)

Here's my code as of now:

String path = db.getImagePath(file_name, curTime);

                            Log.v("Your image filename", file_name);
                            Log.v("Your currentPosition", curTime);

                            Log.v("Your Path Playback edit", path);

                            try{
                                preview.setImageURI(Uri.parse(Environment.getExternalStorageDirectory()+"/Echo/Images/"+file_name));
                            }catch (Exception e){
                                e.printStackTrace();
                                preview.setImageResource(R.drawable.sample_image);
                            }

This runs under a runnable method. and Here's the logcat I get:

V/Your Path: /mnt/sdcard/Echo/Images/
V/Your image filename: Awesome4 - 00:01.jpg
V/Your currentPosition: 00:01
V/Your Path Playback edit: /mnt/sdcard/Echo/Images/
E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /mnt/sdcard/Echo/Images/Awesome4 - 00:01.jpg: open failed: ENOENT (No such file or directory)
I/System.out: resolveUri failed on bad bitmap uri: /mnt/sdcard/Echo/Images/Awesome4 - 00:01.jpg
D/dalvikvm: GC_CONCURRENT freed 380K, 14% free 3067K/3560K, paused 74ms+3ms, total 226ms
V/Your image filename: Awesome4 - 00:02.jpg
V/Your currentPosition: 00:02
V/Your Path Playback edit: [ 06-27 04:14:07.691  6043: 6043 E/BitmapFactory ]
 /mnt/sdcard/Echo/Images/Awesome4 - 00:02.jpg: open failed: ENOENT (No such file or directory)
like image 513
KaHeL Avatar asked Mar 24 '23 09:03

KaHeL


1 Answers

check this if it helps you.

preview.setImageURI(Uri.parse(Environment.getExternalStorageDirectory().getAbsolutePath()+"/Echo/Images/"+file_name));

that how i do it...

public final static String APP_PATH_SD_CARD = "/DesiredSubfolderName/";
public final static String APP_THUMBNAIL_PATH_SD_CARD = "thumbnails";

public boolean saveImageToExternalStorage(Bitmap image) {
String fullPath = Environment.getExternalStorageDirectory().getAbsolutePath() + APP_PATH_SD_CARD + APP_THUMBNAIL_PATH_SD_CARD;

try {
File dir = new File(fullPath);
if (!dir.exists()) {
dir.mkdirs();
}

OutputStream fOut = null;
File file = new File(fullPath, "desiredFilename.png");
file.createNewFile();
fOut = new FileOutputStream(file);

// 100 means no compression, the lower you go, the stronger the compression
image.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();

MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), file.getName(), file.getName());

return true;

} catch (Exception e) {
Log.e("saveToExternalStorage()", e.getMessage());
return false;
}
}
like image 76
Aarun Avatar answered Apr 26 '23 02:04

Aarun