user3776241
user3776241

Reputation: 543

Save & retrieve images from SQLite database

I'm trying to save the path of a downloaded file that was saved to the SD card, but I'm running into troubles when I try to retrieve it. When I try to convert it to a Bitmap, it says the file can't be found, even though it is on the SD card.

File example : /storage/emulated/0/_1404876264453.jpeg

Here's the code that I use to download a photo, and store it to the SD card (returns the URL of where it is saved on the device):

private String downloadProfile(String url)
{
    String imageUrl = null; 

    try 
    {
        String PATH = Environment.getExternalStorageDirectory().getAbsolutePath(); 
        String mimeType = getMimeType(url); 
        String fileExtension = "."+mimeType.replace("image/", "");

        URL u = new URL(url);               

        HttpURLConnection con = (HttpURLConnection) u.openConnection(); 
        con.setRequestMethod("GET"); 

        con.setDoInput(true); 
        con.connect(); 

        long millis=System.currentTimeMillis();    

        File file = new File(PATH); 
        file.mkdirs(); 
        File outputFile = new File(file, "_"+millis); 

        FileOutputStream f = new FileOutputStream(outputFile);
        InputStream in = con.getInputStream(); 
        byte[] buffer = new byte[1024];
        int len1 = 0; 

        while ((len1 = in.read()) > 0){ 
            f.write(buffer, 0, len1); 
        }

        imageUrl = outputFile.getAbsolutePath()+fileExtension; 

        f.close(); 
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } 

    return imageUrl; 
}

After downloading it, I insert the above URL into a SQLite database; it stores fine. I used Astro file manager to check if the file was there, and it was.

Here's the code in my BaseAdapter that takes the above file url and attemps to convert it into a Bitmap:

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;

    String profilePath = result_pos.get("profile_url"); // this just returns the example url
    Bitmap bitmap = BitmapFactory.decodeFile(profilePath, options); 
    imageView.setImageBitmap(bitmap); 

LogCat:

07-08 23:35:17.660: E/BitmapFactory(25463): Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/_1404876264453.jpeg: open failed: ENOENT (No such file or directory)

Upvotes: 1

Views: 2596

Answers (1)

dipali
dipali

Reputation: 11188

check this link.i hope its useful to you. reference link

1)create your own folder and save image name in proper way.
2)save image path in sqlite database
3)retrieve same path for get images.

Upvotes: 2

Related Questions