Save photo via URL in database

5

I'm making an application that needs Facebook's login . I was able to get the data all right, but I can not save the profile photo in the database. I was able to display the photo in ImageView , but when saving from the error.

   Intent intent = getIntent();
   Bundle bundle = intent.getExtras();
   try {
   foto = bundle.getString("fotoperfil");
   if (foto != null) {
   sendFile = new File(foto);
   Log.i("sendfile", "" + sendFile);
   Log.i("fotoface", "" + foto);

   new DownloadImageTask((ImageView) findViewById(R.id.iv_perfil))
                    .execute(foto);


    }
    }catch (Exception e){

    }




   private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
    ImageView bmImage;

    public DownloadImageTask(ImageView bmImage) {
        this.bmImage = bmImage;
        Log.i("foto-face", ""+bmImage);
    }

    protected Bitmap doInBackground(String... urls) {
        String urldisplay = urls[0];
        Log.i("foto-face","urldisplay "+urldisplay);
        Bitmap mIcon11 = null;
        try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            Log.i("foto-face","InputStream "+in);
            mIcon11 = BitmapFactory.decodeStream(in);
            Log.i("foto-face","mIcon11 "+mIcon11);

        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return mIcon11;

    }

    protected void onPostExecute(Bitmap result) {

        bmImage.setImageBitmap(result);
        Log.i("foto-face","bmImage "+bmImage);
    }
}

The error occurs in sendFile , because I'm passing the URL of the photo. I do not know if I should download it to later save, if so, can you help me?

E/AndroidRuntime: FATAL EXCEPTION: main Process: br.com.flirt, PID: 22156 java.lang.NullPointerException
at br.com.flirt.Utils.Util.getMd5FromFile(Util.java:162)
at br.com.flirt.EditarPerfilActivity$4$1.onResponse(EditarPerfilActivity.java:407)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5426)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
    
asked by anonymous 06.10.2016 / 21:04

1 answer

0

Since you are working with Facebook data, you do not need to save the Facebook photo itself in your database, just the photo URL (save as a string).

Saving the photo in your database is a bad idea - unless your application works offline - after all, why would you save the photo in your database, consuming space unnecessarily, if facebook already has it saved on their server? What I suggest, as was said earlier, is that you just save the URL and load it dynamically into your application.

To avoid errors when loading images using AsyncTask, you could use one of several (and great) Android image upload libraries that exist because they are reliable and have several advantages over an AsyncTask that we implement. You can, for example, use the Glide library. See how easy it is to load an image from a URL using this lib. In your onCreate() method, put the following code:

Glide.with(context)
.load("URL-da-imagem.com")
.into(suaImageView);

Ready!

Remembering that every load is asynchronous, so do not risk getting an Application Not Responding

Other libs:

17.10.2016 / 00:42