Save images from the server in my application

1

I'm developing an application that once 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 Picasso library to manipulate my images but I do not know how I can download the file and then save the image in the my directory /data/data/pt.MEU_PROJECTO/ so that I can even% my ImageView 's even if you do not have an internet connection.

I have the following function created to save my images that I built from a tutorial but I do not 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 fetch the image, once it has been saved, to the correct directory to popular my ImageView . I found this code but there were a lot of users who commented that it was not working.

Uri uri = Uri.parse("file:///data/data/pt.MEU_PROJECTO/file.jpg");
image.setImageURI(uri);
    
asked by anonymous 19.01.2015 / 14:35

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);
    
11.06.2015 / 22:34