Save photo via URL to database

Asked

Viewed 426 times

5

I’m making an application that needs the login Facebook. I was able to get all the right data, but I’m not able to save the profile photo in the database. I was able to display the photo on ImageView, but when saving from 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 the sendFile, because I’m passing the photo URL. I don’t know if I should do the download and then save, if yes, you can 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)
  • correct your question friend, when I saw I thought that I just wanted to take a photo of a url and was going to answer that for this, just vc give a screen print of your browser and then open the Gimp and separate the line enedereço and ready, took the photo of the url, ririririri

  • 1

    Paste the error log.

2 answers

0

Whoa, that’s all right!?

Facebook already does the image hosting work, what I recommend is that you save only the URL of the same, recovering in the following format:

mUser.getPhotoUrl().toString() + "?type=large"; //capturando a imagem no tamanho grande (getPhotoUrl() é o metodo de retorno do facebook)

That one mUser is a FirebaseUser

Save the image URL. To upload, use a library to upload, recommend the Glide, but it also has the Picasso

If you want to use the app offline later, Glide can save the image cache, and it will work offline.

GlideApp.with(mContext)
  .load(url)
  .diskCacheStrategy(DiskCacheStrategy.ALL)
  .into(imageView);

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 keeps it saved on their server? What I suggest, as stated earlier, is that you save only the URL and load it dynamically into your application.

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

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

Ready!

Remembering that every load is asynchronous, so there is no danger of you receiving an error from Application Not Responding

Other libs:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.