0

I have certain set of image urls which I need to download and store in internal storage.

Code for downloading and saving image is :

   public void callDownloadImageAsync(int pos) {
        String baseDir = this.getFilesDir().getAbsolutePath() + "/";
        if (allImagesArr.size() > pos) {
            try {
                int count;
                String imageUrl = allImagesArr.get(pos);

                URL url = new URL(imageUrl);
                Utility.printMessage("imageUrl..." + imageUrl);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setReadTimeout(60000);
                connection.setConnectTimeout(60000);
                connection.setDoOutput(true);
                connection.setRequestMethod("GET");
                if (Utility.getToken(this).length() > 0) {
                    Utility.printMessage("@@@ my header for photos " + Utility.getToken(this));
                    try {
                        connection.setRequestProperty("X-CSRF-TOKEN", Utility.getToken(this));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                connection.connect();
                String PATH;
                File imgFile;

//                if (allImagesFileNameArr.get(pos).startsWith("lead_")) {
                if (allImagesFileNameArr.get(pos).startsWith("/")) {
                    imgFile = new File(allImagesFileNameArr.get(pos));
                    PATH = imgFile.getAbsolutePath();
                } else {
                    File folder = new File(baseDir + allImagesFileNameArr.get(pos)).getParentFile();
                    if (!folder.exists())
                        folder.mkdirs();
                    String imageName = allImagesFileNameArr.get(pos);
                    imageName = imageName.substring(imageName.lastIndexOf("/"));

                    imgFile = new File(folder, imageName);
                    PATH = imgFile.getAbsolutePath();
                }

//                }

                InputStream input = new BufferedInputStream(url.openStream());
                OutputStream output = new FileOutputStream(PATH);
                byte data[] = new byte[1024];
                long total = 0;
                while ((count = input.read(data)) != -1) {
                    total += count;
                    //                    publishProgress ((int)(total*100/lenghtOfFile));
                    output.write(data, 0, count);
                }
//                Bitmap bitmap = BitmapFactory.decodeStream(input);
//                Utility.saveToInternalStorage(context, bitmap, PATH);

                output.flush();
                output.close();
                input.close();

                Utility.printMessage("image saved...");
            } catch (IOException e) {
                e.printStackTrace();
                Utility.logException(e);
            }

            callDownloadImageAsync(pos + 1);
        }
    }

I get the messege "image saved" in the log. But when i try to read the bitmap using the path I am getting the following exception :

java.lang.IllegalArgumentException: File /data/user/0/com.app.los_pdapp/files/lead_1/PRAPPL_profile.jpg contains a path separator
04-25 14:28:43.024 21208-21208/com.app.los_pdapp W/System.err:     at android.app.ContextImpl.makeFilename(ContextImpl.java:2413)
04-25 14:28:43.024 21208-21208/com.app.los_pdapp W/System.err:     at android.app.ContextImpl.openFileInput(ContextImpl.java:504)
04-25 14:28:43.024 21208-21208/com.app.los_pdapp W/System.err:     at android.content.ContextWrapper.openFileInput(ContextWrapper.java:193)
04-25 14:28:43.024 21208-21208/com.app.los_pdapp W/System.err:     at com.app.los_pdapp.Util.Utility.readBitmap(Utility.java:977)
04-25 14:28:43.024 21208-21208/com.app.los_pdapp W/System.err:     at com.app.los_pdapp.Fragments.PD.Applicant.DetailView.bindView(DetailView.java:154)
04-25 14:28:43.024 21208-21208/com.app.los_pdapp W/System.err:     at com.app.los_pdapp.Fragments.PD.Applicant.DetailView.onViewCreated(DetailView.java:65)
04-25 14:28:43.024 21208-21208/com.app.los_pdapp W/System.err:     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1314)

My method for reading the bitmap is :

public static Bitmap readBitmap(Context context, String name) {

        Utility.printMessage("name of file..."+name);


        String name1 = name.substring(name.lastIndexOf("/")+1);
        Utility.printMessage("name of file..."+name1);
        Bitmap b = BitmapFactory.decodeFile(name);
        Utility.printMessage("*************** : " + b);
        if (b != null)
            return b;
        try {
            FileInputStream fis = context.openFileInput(name);
//            FileOutputStream fos = new FileOutputStream(name);
            b = BitmapFactory.decodeStream(fis);
            fis.close();
            return b;
        } catch (Exception e) {
            e.printStackTrace();
            Utility.logException(e);
        }
        return null;
    }

I tried using the url connection stream and decoding the bitmap but this solution does not work. Hence can any one suggest whats wrong in above code?

Arshad
  • 1,262
  • 16
  • 25
Siddhesh Pawar
  • 259
  • 4
  • 24
  • You might wanna have a look at this https://stackoverflow.com/questions/5963535/java-lang-illegalargumentexception-contains-a-path-separator – Arshad Apr 25 '18 at 09:18
  • Have a look at this https://stackoverflow.com/a/27054423/8770539. This might be helpful – Santhosh Joseph Apr 25 '18 at 09:20

1 Answers1

0

context.openFileInput(name); doesn't accept paths, only filenames. Use FileInputStream fis = context.openFileInput(new File(name).getName()) instead

Niza Siwale
  • 2,390
  • 1
  • 18
  • 20