Save Image in Internal Storage By using Picasso from Url





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















Please Use This Code for save image in your internal Storage by using Url



//Please Put your Image url In $url

Picasso.get().load($url).into(object : Target{
override fun onPrepareLoad(placeHolderDrawable: Drawable?) {

}

override fun onBitmapFailed(e: java.lang.Exception?, errorDrawable: Drawable?) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}

override fun onBitmapLoaded(bitmap: Bitmap?, from: Picasso.LoadedFrom?) {
try {
val root = Environment.getExternalStorageDirectory().toString()
var myDir = File("$root")

if (!myDir.exists()) {
myDir.mkdirs()
}
val name = Date().toString() + ".jpg"
myDir = File(myDir, name)
val out = FileOutputStream(myDir)
bitmap?.compress(Bitmap.CompressFormat.JPEG, 90, out)

out.flush()
out.close()
} catch (e: Exception) {
// some action
}
}
})


and Image will be saved in sdcard.










share|improve this question

























  • check for permissions

    – Vivek Mishra
    Nov 26 '18 at 11:08











  • post your manifest file here

    – Quick learner
    Nov 26 '18 at 11:09











  • I have given for permission <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    – Ashish
    Nov 26 '18 at 11:09











  • please attach logcat here

    – Quick learner
    Nov 26 '18 at 11:09











  • There is no error in LogCat

    – Ashish
    Nov 26 '18 at 11:10


















0















Please Use This Code for save image in your internal Storage by using Url



//Please Put your Image url In $url

Picasso.get().load($url).into(object : Target{
override fun onPrepareLoad(placeHolderDrawable: Drawable?) {

}

override fun onBitmapFailed(e: java.lang.Exception?, errorDrawable: Drawable?) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}

override fun onBitmapLoaded(bitmap: Bitmap?, from: Picasso.LoadedFrom?) {
try {
val root = Environment.getExternalStorageDirectory().toString()
var myDir = File("$root")

if (!myDir.exists()) {
myDir.mkdirs()
}
val name = Date().toString() + ".jpg"
myDir = File(myDir, name)
val out = FileOutputStream(myDir)
bitmap?.compress(Bitmap.CompressFormat.JPEG, 90, out)

out.flush()
out.close()
} catch (e: Exception) {
// some action
}
}
})


and Image will be saved in sdcard.










share|improve this question

























  • check for permissions

    – Vivek Mishra
    Nov 26 '18 at 11:08











  • post your manifest file here

    – Quick learner
    Nov 26 '18 at 11:09











  • I have given for permission <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    – Ashish
    Nov 26 '18 at 11:09











  • please attach logcat here

    – Quick learner
    Nov 26 '18 at 11:09











  • There is no error in LogCat

    – Ashish
    Nov 26 '18 at 11:10














0












0








0


1






Please Use This Code for save image in your internal Storage by using Url



//Please Put your Image url In $url

Picasso.get().load($url).into(object : Target{
override fun onPrepareLoad(placeHolderDrawable: Drawable?) {

}

override fun onBitmapFailed(e: java.lang.Exception?, errorDrawable: Drawable?) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}

override fun onBitmapLoaded(bitmap: Bitmap?, from: Picasso.LoadedFrom?) {
try {
val root = Environment.getExternalStorageDirectory().toString()
var myDir = File("$root")

if (!myDir.exists()) {
myDir.mkdirs()
}
val name = Date().toString() + ".jpg"
myDir = File(myDir, name)
val out = FileOutputStream(myDir)
bitmap?.compress(Bitmap.CompressFormat.JPEG, 90, out)

out.flush()
out.close()
} catch (e: Exception) {
// some action
}
}
})


and Image will be saved in sdcard.










share|improve this question
















Please Use This Code for save image in your internal Storage by using Url



//Please Put your Image url In $url

Picasso.get().load($url).into(object : Target{
override fun onPrepareLoad(placeHolderDrawable: Drawable?) {

}

override fun onBitmapFailed(e: java.lang.Exception?, errorDrawable: Drawable?) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}

override fun onBitmapLoaded(bitmap: Bitmap?, from: Picasso.LoadedFrom?) {
try {
val root = Environment.getExternalStorageDirectory().toString()
var myDir = File("$root")

if (!myDir.exists()) {
myDir.mkdirs()
}
val name = Date().toString() + ".jpg"
myDir = File(myDir, name)
val out = FileOutputStream(myDir)
bitmap?.compress(Bitmap.CompressFormat.JPEG, 90, out)

out.flush()
out.close()
} catch (e: Exception) {
// some action
}
}
})


and Image will be saved in sdcard.







android kotlin picasso






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 16:11









Suraj Rao

24.1k86074




24.1k86074










asked Nov 26 '18 at 11:07









AshishAshish

685321




685321













  • check for permissions

    – Vivek Mishra
    Nov 26 '18 at 11:08











  • post your manifest file here

    – Quick learner
    Nov 26 '18 at 11:09











  • I have given for permission <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    – Ashish
    Nov 26 '18 at 11:09











  • please attach logcat here

    – Quick learner
    Nov 26 '18 at 11:09











  • There is no error in LogCat

    – Ashish
    Nov 26 '18 at 11:10



















  • check for permissions

    – Vivek Mishra
    Nov 26 '18 at 11:08











  • post your manifest file here

    – Quick learner
    Nov 26 '18 at 11:09











  • I have given for permission <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    – Ashish
    Nov 26 '18 at 11:09











  • please attach logcat here

    – Quick learner
    Nov 26 '18 at 11:09











  • There is no error in LogCat

    – Ashish
    Nov 26 '18 at 11:10

















check for permissions

– Vivek Mishra
Nov 26 '18 at 11:08





check for permissions

– Vivek Mishra
Nov 26 '18 at 11:08













post your manifest file here

– Quick learner
Nov 26 '18 at 11:09





post your manifest file here

– Quick learner
Nov 26 '18 at 11:09













I have given for permission <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

– Ashish
Nov 26 '18 at 11:09





I have given for permission <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

– Ashish
Nov 26 '18 at 11:09













please attach logcat here

– Quick learner
Nov 26 '18 at 11:09





please attach logcat here

– Quick learner
Nov 26 '18 at 11:09













There is no error in LogCat

– Ashish
Nov 26 '18 at 11:10





There is no error in LogCat

– Ashish
Nov 26 '18 at 11:10












2 Answers
2






active

oldest

votes


















1














1- add to AndroidManifest.xml



<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


2- Use this method to download image using Picasso from Url:



    private static void SaveImage(final Context context, final String MyUrl){
final ProgressDialog progress = new ProgressDialog(context);
class SaveThisImage extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
progress.setTitle("Processing");
progress.setMessage("Please Wait...");
progress.setCancelable(false);
progress.show();
}
@Override
protected Void doInBackground(Void... arg0) {
try{

File sdCard = Environment.getExternalStorageDirectory();
@SuppressLint("DefaultLocale") String fileName = String.format("%d.jpg", System.currentTimeMillis());
File dir = new File(sdCard.getAbsolutePath() + "/savedImageName");
dir.mkdirs();
final File myImageFile = new File(dir, fileName); // Create image file
FileOutputStream fos = null;
try {
fos = new FileOutputStream(myImageFile);
Bitmap bitmap = Picasso.get().load(MyUrl).get();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);

Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(myImageFile));
context.sendBroadcast(intent);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}catch (Exception e){
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if(progress.isShowing()){
progress.dismiss();
}
Toast.makeText(context, "Saved", Toast.LENGTH_SHORT).show();
}
}
SaveThisImage shareimg = new SaveThisImage();
shareimg.execute();
}


3- how to use, just call:



SaveImage(context, "Image URL";





share|improve this answer































    1














    Use this method to download image in external memory



     // DownloadImage AsyncTask
    private class DownloadImage extends AsyncTask<String, Void, Bitmap> {

    @Override
    protected void onPreExecute() {
    super.onPreExecute();

    }

    @Override
    protected Bitmap doInBackground(String... URL) {

    String imageURL = URL[0];

    Bitmap bitmap = null;
    try {
    // Download Image from URL
    InputStream input = new java.net.URL(imageURL).openStream();
    // Decode Bitmap
    bitmap = BitmapFactory.decodeStream(input);
    } catch (Exception e) {
    e.printStackTrace();
    }
    return bitmap;
    }

    @Override
    protected void onPostExecute(Bitmap result) {

    if (result != null) {
    File destination = new File(getActivity().getCacheDir(),
    "profile" + ".jpg");
    try {
    destination.createNewFile();
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    result.compress(Bitmap.CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
    byte bitmapdata = bos.toByteArray();

    FileOutputStream fos = new FileOutputStream(destination);
    fos.write(bitmapdata);
    fos.flush();
    fos.close();
    selectedFile = destination;
    } catch (IOException e) {
    e.printStackTrace();
    }
    }



    }
    }


    And call this method like this



    Picasso.get().load(response.body()?.url).into(object : Target{
    override fun onPrepareLoad(placeHolderDrawable: Drawable?) {

    }

    override fun onBitmapFailed(e: java.lang.Exception?, errorDrawable: Drawable?) {
    TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun onBitmapLoaded(bitmap: Bitmap?, from: Picasso.LoadedFrom?) {
    new DownloadImage().execute("url_here);
    }
    })


    **




    Note:- Copy this code and paste it , Kotlin converter will
    automatically convert it to Kotlin




    **






    share|improve this answer
























    • can you tell me how to store in the app location ?

      – Ashish
      Nov 26 '18 at 11:21











    • it will show in main diectory , where your DCIM,Music , Picture folders are

      – Quick learner
      Nov 26 '18 at 11:22













    • yes i want to save in app location means in data>data>app

      – Ashish
      Nov 26 '18 at 11:24











    • do you mean internal memory ?

      – Quick learner
      Nov 26 '18 at 11:26






    • 1





      Android>data>com.package.name = this is the app internal memory allotted and can save it there.

      – Quick learner
      Nov 26 '18 at 11:28












    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53479820%2fsave-image-in-internal-storage-by-using-picasso-from-url%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    1- add to AndroidManifest.xml



    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


    2- Use this method to download image using Picasso from Url:



        private static void SaveImage(final Context context, final String MyUrl){
    final ProgressDialog progress = new ProgressDialog(context);
    class SaveThisImage extends AsyncTask<Void, Void, Void> {
    @Override
    protected void onPreExecute() {
    super.onPreExecute();
    progress.setTitle("Processing");
    progress.setMessage("Please Wait...");
    progress.setCancelable(false);
    progress.show();
    }
    @Override
    protected Void doInBackground(Void... arg0) {
    try{

    File sdCard = Environment.getExternalStorageDirectory();
    @SuppressLint("DefaultLocale") String fileName = String.format("%d.jpg", System.currentTimeMillis());
    File dir = new File(sdCard.getAbsolutePath() + "/savedImageName");
    dir.mkdirs();
    final File myImageFile = new File(dir, fileName); // Create image file
    FileOutputStream fos = null;
    try {
    fos = new FileOutputStream(myImageFile);
    Bitmap bitmap = Picasso.get().load(MyUrl).get();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);

    Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    intent.setData(Uri.fromFile(myImageFile));
    context.sendBroadcast(intent);
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    try {
    fos.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }catch (Exception e){
    }
    return null;
    }
    @Override
    protected void onPostExecute(Void result) {
    super.onPostExecute(result);
    if(progress.isShowing()){
    progress.dismiss();
    }
    Toast.makeText(context, "Saved", Toast.LENGTH_SHORT).show();
    }
    }
    SaveThisImage shareimg = new SaveThisImage();
    shareimg.execute();
    }


    3- how to use, just call:



    SaveImage(context, "Image URL";





    share|improve this answer




























      1














      1- add to AndroidManifest.xml



      <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
      <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


      2- Use this method to download image using Picasso from Url:



          private static void SaveImage(final Context context, final String MyUrl){
      final ProgressDialog progress = new ProgressDialog(context);
      class SaveThisImage extends AsyncTask<Void, Void, Void> {
      @Override
      protected void onPreExecute() {
      super.onPreExecute();
      progress.setTitle("Processing");
      progress.setMessage("Please Wait...");
      progress.setCancelable(false);
      progress.show();
      }
      @Override
      protected Void doInBackground(Void... arg0) {
      try{

      File sdCard = Environment.getExternalStorageDirectory();
      @SuppressLint("DefaultLocale") String fileName = String.format("%d.jpg", System.currentTimeMillis());
      File dir = new File(sdCard.getAbsolutePath() + "/savedImageName");
      dir.mkdirs();
      final File myImageFile = new File(dir, fileName); // Create image file
      FileOutputStream fos = null;
      try {
      fos = new FileOutputStream(myImageFile);
      Bitmap bitmap = Picasso.get().load(MyUrl).get();
      bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);

      Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
      intent.setData(Uri.fromFile(myImageFile));
      context.sendBroadcast(intent);
      } catch (IOException e) {
      e.printStackTrace();
      } finally {
      try {
      fos.close();
      } catch (IOException e) {
      e.printStackTrace();
      }
      }
      }catch (Exception e){
      }
      return null;
      }
      @Override
      protected void onPostExecute(Void result) {
      super.onPostExecute(result);
      if(progress.isShowing()){
      progress.dismiss();
      }
      Toast.makeText(context, "Saved", Toast.LENGTH_SHORT).show();
      }
      }
      SaveThisImage shareimg = new SaveThisImage();
      shareimg.execute();
      }


      3- how to use, just call:



      SaveImage(context, "Image URL";





      share|improve this answer


























        1












        1








        1







        1- add to AndroidManifest.xml



        <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


        2- Use this method to download image using Picasso from Url:



            private static void SaveImage(final Context context, final String MyUrl){
        final ProgressDialog progress = new ProgressDialog(context);
        class SaveThisImage extends AsyncTask<Void, Void, Void> {
        @Override
        protected void onPreExecute() {
        super.onPreExecute();
        progress.setTitle("Processing");
        progress.setMessage("Please Wait...");
        progress.setCancelable(false);
        progress.show();
        }
        @Override
        protected Void doInBackground(Void... arg0) {
        try{

        File sdCard = Environment.getExternalStorageDirectory();
        @SuppressLint("DefaultLocale") String fileName = String.format("%d.jpg", System.currentTimeMillis());
        File dir = new File(sdCard.getAbsolutePath() + "/savedImageName");
        dir.mkdirs();
        final File myImageFile = new File(dir, fileName); // Create image file
        FileOutputStream fos = null;
        try {
        fos = new FileOutputStream(myImageFile);
        Bitmap bitmap = Picasso.get().load(MyUrl).get();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);

        Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        intent.setData(Uri.fromFile(myImageFile));
        context.sendBroadcast(intent);
        } catch (IOException e) {
        e.printStackTrace();
        } finally {
        try {
        fos.close();
        } catch (IOException e) {
        e.printStackTrace();
        }
        }
        }catch (Exception e){
        }
        return null;
        }
        @Override
        protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        if(progress.isShowing()){
        progress.dismiss();
        }
        Toast.makeText(context, "Saved", Toast.LENGTH_SHORT).show();
        }
        }
        SaveThisImage shareimg = new SaveThisImage();
        shareimg.execute();
        }


        3- how to use, just call:



        SaveImage(context, "Image URL";





        share|improve this answer













        1- add to AndroidManifest.xml



        <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


        2- Use this method to download image using Picasso from Url:



            private static void SaveImage(final Context context, final String MyUrl){
        final ProgressDialog progress = new ProgressDialog(context);
        class SaveThisImage extends AsyncTask<Void, Void, Void> {
        @Override
        protected void onPreExecute() {
        super.onPreExecute();
        progress.setTitle("Processing");
        progress.setMessage("Please Wait...");
        progress.setCancelable(false);
        progress.show();
        }
        @Override
        protected Void doInBackground(Void... arg0) {
        try{

        File sdCard = Environment.getExternalStorageDirectory();
        @SuppressLint("DefaultLocale") String fileName = String.format("%d.jpg", System.currentTimeMillis());
        File dir = new File(sdCard.getAbsolutePath() + "/savedImageName");
        dir.mkdirs();
        final File myImageFile = new File(dir, fileName); // Create image file
        FileOutputStream fos = null;
        try {
        fos = new FileOutputStream(myImageFile);
        Bitmap bitmap = Picasso.get().load(MyUrl).get();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);

        Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        intent.setData(Uri.fromFile(myImageFile));
        context.sendBroadcast(intent);
        } catch (IOException e) {
        e.printStackTrace();
        } finally {
        try {
        fos.close();
        } catch (IOException e) {
        e.printStackTrace();
        }
        }
        }catch (Exception e){
        }
        return null;
        }
        @Override
        protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        if(progress.isShowing()){
        progress.dismiss();
        }
        Toast.makeText(context, "Saved", Toast.LENGTH_SHORT).show();
        }
        }
        SaveThisImage shareimg = new SaveThisImage();
        shareimg.execute();
        }


        3- how to use, just call:



        SaveImage(context, "Image URL";






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 26 '18 at 23:48









        AbdomnsAbdomns

        40018




        40018

























            1














            Use this method to download image in external memory



             // DownloadImage AsyncTask
            private class DownloadImage extends AsyncTask<String, Void, Bitmap> {

            @Override
            protected void onPreExecute() {
            super.onPreExecute();

            }

            @Override
            protected Bitmap doInBackground(String... URL) {

            String imageURL = URL[0];

            Bitmap bitmap = null;
            try {
            // Download Image from URL
            InputStream input = new java.net.URL(imageURL).openStream();
            // Decode Bitmap
            bitmap = BitmapFactory.decodeStream(input);
            } catch (Exception e) {
            e.printStackTrace();
            }
            return bitmap;
            }

            @Override
            protected void onPostExecute(Bitmap result) {

            if (result != null) {
            File destination = new File(getActivity().getCacheDir(),
            "profile" + ".jpg");
            try {
            destination.createNewFile();
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            result.compress(Bitmap.CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
            byte bitmapdata = bos.toByteArray();

            FileOutputStream fos = new FileOutputStream(destination);
            fos.write(bitmapdata);
            fos.flush();
            fos.close();
            selectedFile = destination;
            } catch (IOException e) {
            e.printStackTrace();
            }
            }



            }
            }


            And call this method like this



            Picasso.get().load(response.body()?.url).into(object : Target{
            override fun onPrepareLoad(placeHolderDrawable: Drawable?) {

            }

            override fun onBitmapFailed(e: java.lang.Exception?, errorDrawable: Drawable?) {
            TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
            }

            override fun onBitmapLoaded(bitmap: Bitmap?, from: Picasso.LoadedFrom?) {
            new DownloadImage().execute("url_here);
            }
            })


            **




            Note:- Copy this code and paste it , Kotlin converter will
            automatically convert it to Kotlin




            **






            share|improve this answer
























            • can you tell me how to store in the app location ?

              – Ashish
              Nov 26 '18 at 11:21











            • it will show in main diectory , where your DCIM,Music , Picture folders are

              – Quick learner
              Nov 26 '18 at 11:22













            • yes i want to save in app location means in data>data>app

              – Ashish
              Nov 26 '18 at 11:24











            • do you mean internal memory ?

              – Quick learner
              Nov 26 '18 at 11:26






            • 1





              Android>data>com.package.name = this is the app internal memory allotted and can save it there.

              – Quick learner
              Nov 26 '18 at 11:28
















            1














            Use this method to download image in external memory



             // DownloadImage AsyncTask
            private class DownloadImage extends AsyncTask<String, Void, Bitmap> {

            @Override
            protected void onPreExecute() {
            super.onPreExecute();

            }

            @Override
            protected Bitmap doInBackground(String... URL) {

            String imageURL = URL[0];

            Bitmap bitmap = null;
            try {
            // Download Image from URL
            InputStream input = new java.net.URL(imageURL).openStream();
            // Decode Bitmap
            bitmap = BitmapFactory.decodeStream(input);
            } catch (Exception e) {
            e.printStackTrace();
            }
            return bitmap;
            }

            @Override
            protected void onPostExecute(Bitmap result) {

            if (result != null) {
            File destination = new File(getActivity().getCacheDir(),
            "profile" + ".jpg");
            try {
            destination.createNewFile();
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            result.compress(Bitmap.CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
            byte bitmapdata = bos.toByteArray();

            FileOutputStream fos = new FileOutputStream(destination);
            fos.write(bitmapdata);
            fos.flush();
            fos.close();
            selectedFile = destination;
            } catch (IOException e) {
            e.printStackTrace();
            }
            }



            }
            }


            And call this method like this



            Picasso.get().load(response.body()?.url).into(object : Target{
            override fun onPrepareLoad(placeHolderDrawable: Drawable?) {

            }

            override fun onBitmapFailed(e: java.lang.Exception?, errorDrawable: Drawable?) {
            TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
            }

            override fun onBitmapLoaded(bitmap: Bitmap?, from: Picasso.LoadedFrom?) {
            new DownloadImage().execute("url_here);
            }
            })


            **




            Note:- Copy this code and paste it , Kotlin converter will
            automatically convert it to Kotlin




            **






            share|improve this answer
























            • can you tell me how to store in the app location ?

              – Ashish
              Nov 26 '18 at 11:21











            • it will show in main diectory , where your DCIM,Music , Picture folders are

              – Quick learner
              Nov 26 '18 at 11:22













            • yes i want to save in app location means in data>data>app

              – Ashish
              Nov 26 '18 at 11:24











            • do you mean internal memory ?

              – Quick learner
              Nov 26 '18 at 11:26






            • 1





              Android>data>com.package.name = this is the app internal memory allotted and can save it there.

              – Quick learner
              Nov 26 '18 at 11:28














            1












            1








            1







            Use this method to download image in external memory



             // DownloadImage AsyncTask
            private class DownloadImage extends AsyncTask<String, Void, Bitmap> {

            @Override
            protected void onPreExecute() {
            super.onPreExecute();

            }

            @Override
            protected Bitmap doInBackground(String... URL) {

            String imageURL = URL[0];

            Bitmap bitmap = null;
            try {
            // Download Image from URL
            InputStream input = new java.net.URL(imageURL).openStream();
            // Decode Bitmap
            bitmap = BitmapFactory.decodeStream(input);
            } catch (Exception e) {
            e.printStackTrace();
            }
            return bitmap;
            }

            @Override
            protected void onPostExecute(Bitmap result) {

            if (result != null) {
            File destination = new File(getActivity().getCacheDir(),
            "profile" + ".jpg");
            try {
            destination.createNewFile();
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            result.compress(Bitmap.CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
            byte bitmapdata = bos.toByteArray();

            FileOutputStream fos = new FileOutputStream(destination);
            fos.write(bitmapdata);
            fos.flush();
            fos.close();
            selectedFile = destination;
            } catch (IOException e) {
            e.printStackTrace();
            }
            }



            }
            }


            And call this method like this



            Picasso.get().load(response.body()?.url).into(object : Target{
            override fun onPrepareLoad(placeHolderDrawable: Drawable?) {

            }

            override fun onBitmapFailed(e: java.lang.Exception?, errorDrawable: Drawable?) {
            TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
            }

            override fun onBitmapLoaded(bitmap: Bitmap?, from: Picasso.LoadedFrom?) {
            new DownloadImage().execute("url_here);
            }
            })


            **




            Note:- Copy this code and paste it , Kotlin converter will
            automatically convert it to Kotlin




            **






            share|improve this answer













            Use this method to download image in external memory



             // DownloadImage AsyncTask
            private class DownloadImage extends AsyncTask<String, Void, Bitmap> {

            @Override
            protected void onPreExecute() {
            super.onPreExecute();

            }

            @Override
            protected Bitmap doInBackground(String... URL) {

            String imageURL = URL[0];

            Bitmap bitmap = null;
            try {
            // Download Image from URL
            InputStream input = new java.net.URL(imageURL).openStream();
            // Decode Bitmap
            bitmap = BitmapFactory.decodeStream(input);
            } catch (Exception e) {
            e.printStackTrace();
            }
            return bitmap;
            }

            @Override
            protected void onPostExecute(Bitmap result) {

            if (result != null) {
            File destination = new File(getActivity().getCacheDir(),
            "profile" + ".jpg");
            try {
            destination.createNewFile();
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            result.compress(Bitmap.CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
            byte bitmapdata = bos.toByteArray();

            FileOutputStream fos = new FileOutputStream(destination);
            fos.write(bitmapdata);
            fos.flush();
            fos.close();
            selectedFile = destination;
            } catch (IOException e) {
            e.printStackTrace();
            }
            }



            }
            }


            And call this method like this



            Picasso.get().load(response.body()?.url).into(object : Target{
            override fun onPrepareLoad(placeHolderDrawable: Drawable?) {

            }

            override fun onBitmapFailed(e: java.lang.Exception?, errorDrawable: Drawable?) {
            TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
            }

            override fun onBitmapLoaded(bitmap: Bitmap?, from: Picasso.LoadedFrom?) {
            new DownloadImage().execute("url_here);
            }
            })


            **




            Note:- Copy this code and paste it , Kotlin converter will
            automatically convert it to Kotlin




            **







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 26 '18 at 11:20









            Quick learnerQuick learner

            2,66511027




            2,66511027













            • can you tell me how to store in the app location ?

              – Ashish
              Nov 26 '18 at 11:21











            • it will show in main diectory , where your DCIM,Music , Picture folders are

              – Quick learner
              Nov 26 '18 at 11:22













            • yes i want to save in app location means in data>data>app

              – Ashish
              Nov 26 '18 at 11:24











            • do you mean internal memory ?

              – Quick learner
              Nov 26 '18 at 11:26






            • 1





              Android>data>com.package.name = this is the app internal memory allotted and can save it there.

              – Quick learner
              Nov 26 '18 at 11:28



















            • can you tell me how to store in the app location ?

              – Ashish
              Nov 26 '18 at 11:21











            • it will show in main diectory , where your DCIM,Music , Picture folders are

              – Quick learner
              Nov 26 '18 at 11:22













            • yes i want to save in app location means in data>data>app

              – Ashish
              Nov 26 '18 at 11:24











            • do you mean internal memory ?

              – Quick learner
              Nov 26 '18 at 11:26






            • 1





              Android>data>com.package.name = this is the app internal memory allotted and can save it there.

              – Quick learner
              Nov 26 '18 at 11:28

















            can you tell me how to store in the app location ?

            – Ashish
            Nov 26 '18 at 11:21





            can you tell me how to store in the app location ?

            – Ashish
            Nov 26 '18 at 11:21













            it will show in main diectory , where your DCIM,Music , Picture folders are

            – Quick learner
            Nov 26 '18 at 11:22







            it will show in main diectory , where your DCIM,Music , Picture folders are

            – Quick learner
            Nov 26 '18 at 11:22















            yes i want to save in app location means in data>data>app

            – Ashish
            Nov 26 '18 at 11:24





            yes i want to save in app location means in data>data>app

            – Ashish
            Nov 26 '18 at 11:24













            do you mean internal memory ?

            – Quick learner
            Nov 26 '18 at 11:26





            do you mean internal memory ?

            – Quick learner
            Nov 26 '18 at 11:26




            1




            1





            Android>data>com.package.name = this is the app internal memory allotted and can save it there.

            – Quick learner
            Nov 26 '18 at 11:28





            Android>data>com.package.name = this is the app internal memory allotted and can save it there.

            – Quick learner
            Nov 26 '18 at 11:28


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53479820%2fsave-image-in-internal-storage-by-using-picasso-from-url%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Wiesbaden

            Marschland

            Dieringhausen