0

I want to download images from server.I have tried with below code but only .png format images are download , remain .jpeg, .gif, .bmp etc are not. Can someone help me how to download all type of image format's in my Android Application.Thanks in advanced.

This is my Download method

void download_PngFile(String fileUrl)
    {
        Log.e("In download_PngFile ", " str_imgList_imageaudioPath = " + imageName);
        Bitmap imagenObtenida = null;
        try {
            URL ImgUrl = new URL(fileUrl);
            HttpURLConnection conn = (HttpURLConnection) ImgUrl.openConnection();
            conn.connect();
            imagenObtenida = BitmapFactory.decodeStream(conn.getInputStream());
            Log.e("imagenObtenida", " = " + imagenObtenida);

            String fotoname = imageName;
            File file = new File(newFolder, fotoname);
            int sizeOfImage = (int) file.length();
            Log.e("sizeOfImage "," = "+ sizeOfImage +"@ "+ imageName);
            if (file.exists()) file.delete();
            try {
                FileOutputStream out = new FileOutputStream(file);
                imagenObtenida.compress(Bitmap.CompressFormat.PNG, 90, out);
                out.flush();
                out.close();
                Log.e("Png = ","DownLoad complete");

            } catch (Exception e) {

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

Here is my another method which i checked in if condition images are ends with png, gif, bmp, and jpeg

public void getDoenLoaddata()
        {
            Log.e("Record is exists !!", " == ");
            //CreateFile();
            dbhelper = new MyDbHelper(this);
            SQLiteDatabase db1 = dbhelper.getReadableDatabase();

            Cursor cursor = db1.rawQuery("select * from ActivityObjectList", null);

            if (cursor.moveToFirst())
            {
                do
                {
                    imageName = cursor.getString(cursor.getColumnIndex("imageaudioPath"));
                    String strDownLoadStatus = cursor.getString(cursor.getColumnIndex("DownLoad_Status"));
                    //Log.e("imageName ", "  = " + imageName + " & strDownLoadStatus is " + strDownLoadStatus);
                    if (strDownLoadStatus.equalsIgnoreCase("0"))
                    {
                        Log.e("Insert", " Status One");

                        if (imageName.endsWith(mp3_Pattern))
                        {
                            String str_Mp3Url = "http://103.24.4.60/CLASSNK1/MobileService.svc/DownloadFile/FileName/"+imageName;
                            Log.e("str_ImageUrl#####", "  = " + str_Mp3Url);

                            download_Mp3File(str_Mp3Url);
                            strDownLoadStatus = "1";
                            dbhelper.update_DownLoadStatus(imageName, strDownLoadStatus);
                        }

                        if (imageName.endsWith(png_Pattern))
                        {
                            Log.e("imageName", " Status One" + imageName);
                            String str_ImageUrl = "http://103.24.4.60/CLASSNK1/MobileService.svc/DownloadFile/FileName/"+imageName;
                            Log.e("png_Pattern  ", " str_ImageUrl = " + str_ImageUrl);
                            download_PngFile(str_ImageUrl);

                            strDownLoadStatus = "1";
                            dbhelper.update_DownLoadStatus(imageName, strDownLoadStatus);
                        }

                        if (imageName.endsWith(jpg_pattern))
                        {
                            String str_Mp3Url = "http://103.24.4.60/CLASSNK1/MobileService.svc/DownloadFile/FileName/"+imageName;
                            Log.e("str_ImageUrl#####", "  = " + str_Mp3Url);

                            download_Mp3File(str_Mp3Url);
                            strDownLoadStatus = "1";
                            dbhelper.update_DownLoadStatus(imageName, strDownLoadStatus);
                        }

                        if (imageName.endsWith(jpeg_pattern))
                        {
                            String str_Mp3Url = "http://103.24.4.60/CLASSNK1/MobileService.svc/DownloadFile/FileName/"+imageName;
                            Log.e("str_ImageUrl#####", "  = " + str_Mp3Url);

                            download_Mp3File(str_Mp3Url);
                            strDownLoadStatus = "1";
                            dbhelper.update_DownLoadStatus(imageName, strDownLoadStatus);
                        }

                        if (imageName.endsWith(gif_pattern))
                        {
                            String str_Mp3Url = "http://103.24.4.60/CLASSNK1/MobileService.svc/DownloadFile/FileName/"+imageName;
                            Log.e("str_ImageUrl#####", "  = " + str_Mp3Url);

                            download_Mp3File(str_Mp3Url);
                            strDownLoadStatus = "1";
                            dbhelper.update_DownLoadStatus(imageName, strDownLoadStatus);
                        }
                        if (imageName.endsWith(bmp_pattern))
                        {
                            String str_Mp3Url = "http://103.24.4.60/CLASSNK1/MobileService.svc/DownloadFile/FileName/"+imageName;
                            Log.e("str_ImageUrl#####", "  = " + str_Mp3Url);

                            download_Mp3File(str_Mp3Url);
                            strDownLoadStatus = "1";
                            dbhelper.update_DownLoadStatus(imageName, strDownLoadStatus);
                        }
                    }
                    else if (strDownLoadStatus.equalsIgnoreCase("1"))
                    {
                        // Log.e("Nothind To","Here !! ");
                    }

                } while (cursor.moveToNext());
            } cursor.close();
            db1.close();
           /* db1.close();*/
    }
androidTag
  • 5,053
  • 7
  • 19
  • 28
  • So you want to download all the images and query in the scard from them later or what exactly? – Andrei T Sep 19 '15 at 07:23
  • 1
    Did you copy-pasted these codes from somewhere without reading them? The first explicitly says to save images in png format, the second downloads **images** with function named "Download_mp3file". – Psytho Sep 19 '15 at 07:28

1 Answers1

1

You should try to search and find your files based on the mimetyes.
At the following posts you can find help in querying images media and non-media using MediaStore and second when you want from a specific folder using again MediaStore class.
1. MediaStore - Uri to query all types of files (media and non-media).
2. Displaying images from a specific folder on the SDCard using a gridview.

Community
  • 1
  • 1
Andrei T
  • 2,985
  • 3
  • 21
  • 28