Save server images to my application

Asked

Viewed 1,036 times

1

I am developing an application that after installed, the first time it is initialized it goes to the server to fetch all the images necessary for the user to use the application.

I chose the library Picasso to manipulate my images but I don’t know how to download the file and then save the image in my directory /data/data/pt.MEU_PROJECTO/ so I can popular mine ImageView'even if the user has no internet connection.

I have the following function created to save my images that I built from a tutorial but I don’t know which parameter of the Picasso class I should pass or which directory is being used to save the images.

public static void saveAssetImage(Context context, String assetName){
    File fileDirectory = context.getFilesDir();
    File fileToWrite = new File(fileDirectory, assetName);

    AssetManager assetManager = context.getAssets();
    try{
        InputStream in = assetManager.open(assetName);
        FileOutputStream out = new FileOutputStream(fileToWrite);

        in.close();
        out.close();
    }catch (IOException e){
        e.printStackTrace();
    }
} 

saveAssetImage(getApplicationContext(), Picasso.with(this).load(urlImagem).fetch());

I also need to know how I can get the image, once it’s stored, to the correct directory to popular my ImageView. I found this code but there were many users who will comment not working.

Uri uri = Uri.parse("file:///data/data/pt.MEU_PROJECTO/file.jpg");
image.setImageURI(uri);
  • This might help http://answall.com/questions/44092/como-salvar-recuperar-imagem-na-mem%C3%B3ria-no-android

1 answer

2

To save the image:

    Picasso.with(context).load(URL IMAGEM).into(target);

 private Target target = new Target() {
        @Override
        public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    String pathImage = Environment.getExternalStorageDirectory().getPath() + "/PASTA ONDE ESTÁ A IMGEM";

                    File projDir = new File(dirPath);

                    if (!projDir.exists()) {
                        projDir.mkdirs();
                    }

                    File file = new File(dirPath + File.separator + "NOME DA IMAGEM");

                    try {
                        if (file.exists()) {
                            file.delete();
                        }
                        file.createNewFile();
                        FileOutputStream ostream = new FileOutputStream(file);
                        bitmap.compress(Bitmap.CompressFormat.JPEG, 75, ostream);
                        ostream.close();

                        user.setUrlPath(file.getAbsolutePath());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }
            }).start();
        }

        @Override
        public void onBitmapFailed(Drawable errorDrawable) {
        }

        @Override
        public void onPrepareLoad(Drawable placeHolderDrawable) {
            if (placeHolderDrawable != null) {
            }
        }
    };

To recover the image use the following:

String pathImage = Environment.getExternalStorageDirectory().getPath() + "/PASTA ONDE ESTÁ A IMGEM/NOME DA IMAGEM";

File f = new File(pathImage);
Picasso.with(context).load(f).into(this.mUserPhoto);

Browser other questions tagged

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