2

I am getting a person's facebook profile picture, and I want to save it into my local storage so it can be recovered and shown later.

I did everything this link said, but I am getting an exception on the BitmapFactory.decodeStream line AND I can't see what the exception is, when I debug, the debugger goes directly to the "return null" line and I can't evaluate the "e" variable.

here's my code:

InternalFileHelper.cs:

public void saveToInternalSorage(File mypath, Bitmap bitmapImage){
        FileOutputStream fos = null;
        try {
            if (!mypath.exists()) {
                fos = new FileOutputStream(mypath);

                // Use the compress method on the BitMap object to write image to the OutputStream
                bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
                fos.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        //return directory.getAbsolutePath();
    }

    public Bitmap loadImageFromStorage(File f)
    {
        try {
            if (f.exists()) {

                Bitmap b = BitmapFactory.decodeStream(new FileInputStream(f));
                return b;
            }else
                return null;
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
            String mess = e.getMessage();
            return null;
        }
    }

Here's how I save the image:

ContextWrapper cw = new ContextWrapper(getApplicationContext());
// path to /data/data/yourapp/app_data/imageDir
File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
// Create imageDir
File mypath=new File(directory,this.fbId.toString()+".png");

InputStream inputStream;
HttpGet httpRequest = new HttpGet(URI.create("http://graph.facebook.com/" + this.fbId.toString() + "/picture?type=normal"));
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
HttpEntity entity = response.getEntity();
BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
Bitmap bmp = BitmapFactory.decodeStream(bufHttpEntity.getContent());
httpRequest.abort();

this.image = bmp;
helper.saveToInternalSorage(mypath, bmp);

and here's how I recover the image:

ContextWrapper cw = new ContextWrapper(getApplicationContext());
// path to /data/data/yourapp/app_data/imageDir
File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
// Create imageDir
File mypath=new File(directory,this.fbId.toString()+".png");

InternalFileHelper helper = new InternalFileHelper();
Bitmap image = helper.loadImageFromStorage(mypath);

It's driving me nuts!! thanks in advance!!

Community
  • 1
  • 1
Nicole
  • 1,356
  • 3
  • 21
  • 41
  • First of all: please correct the code indentation. Second: in the loadImageFromStorage() method make only one return statement at the very end of the method. This generally eases debugging. Third: add another catch clause with `Exception`, or replace `FileNotFoundException` with `Exception` and debug again. – SME_Dev Oct 02 '14 at 00:19
  • I did all of that, 2 catch and only one with Exception. But still no luck to see the error. – Nicole Oct 02 '14 at 00:54

0 Answers0