mardi 4 août 2015

save images in sd card from server in Android

I want to download images from server and save into sd card in Emulator.First i have to get json string of images and passing to the download server url for download and save into sd card. I m getting error of nullPointer exception at the run time.Please someone help me how to do this.

Here is my Code

public void getAllImagePath_List() {
        String strUrl_GetAll_Activity = "http://xxx/xxx/xxx.svc/xxx/xx/"+str_UserId+"/xx/"+xxx+"/xxx/"+xxx;
        Log.e("strUrl_GetAll_Activity ", " = " + strUrl_GetAll_Activity);
        InputStream inputStream = null;

        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpResponse httpResponse = httpclient.execute(new HttpGet(strUrl_GetAll_Activity));
            inputStream = httpResponse.getEntity().getContent();

            if (inputStream != null)
                strResult = convertInputStreamToString(inputStream);
            else
                strResult = "Did not work!";

            String jsonStr = strResult;

            try {

                JSONObject jsonObj = new JSONObject(jsonStr);
                String jsonResult = jsonObj.toString().trim();
                Log.e("jsonResult ", " = " + jsonResult);

                JSONObject getAllActivity_List = jsonObj.getJSONObject("Get_ALL_ActivityListResult");
                Log.e("getAllActivity_List ", " = " + getAllActivity_List.toString());

                String strSync = getAllActivity_List.getString("Sync_Time");
                Log.e("strSync ", " = " + strSync.toString());

                JSONArray jarr = getAllActivity_List.getJSONArray("ActivityObjectList");
                Log.e("jarr ", " = " + jarr.toString());

                SQLiteDatabase mDb = dbhelper.getWritableDatabase();
                int imageNum = 0;
                BufferedOutputStream bos;
                 imagesArray = new String[jarr.length()];
                for (int j = 0; j < jarr.length(); j++)
                {
                    JSONObject jobjVessels = jarr.getJSONObject(j);

                    str_imgList_imageaudioPath = jobjVessels.getString("imageaudioPath");
                    Log.e("", "" + str_imgList_imageaudioPath);


                    String str_ImageUrl = "http://xxx/xxx/xxx.svc/DownloadFile/FileName/"+str_imgList_imageaudioPath;
                    downloadUrl(str_ImageUrl);

                    Log.e("Downloaded Completed","!!!!!");

                }

            }

            catch (JSONException je)
            {
                je.printStackTrace();
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    private Bitmap downloadUrl(String strUrl) throws IOException{
        Bitmap bitmap=null;
        InputStream iStream = null;
        try{
            URL url = new URL(strUrl);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.connect();
            iStream = urlConnection.getInputStream();
            bitmap = BitmapFactory.decodeStream(iStream);
            Log.e("bitmap while  url", "downloading " + bitmap.toString());



            saveBitmap(bitmap);
        }catch(Exception e){
            Log.e("Exception while  url", "downloading " + e.toString());
        }finally{
            iStream.close();
        }

        return bitmap;
    }


    public void saveBitmap(Bitmap bm)
    {
        try
        {
            String mBaseFolderPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/DCIM/json_imglist";
            String mFilePath = mBaseFolderPath + "abcd.jpg";

            FileOutputStream stream = new FileOutputStream(mFilePath);
            bm.compress(Bitmap.CompressFormat.JPEG, 100, stream);
            stream.flush();
            stream.close();
        }
        catch(Exception e)
        {
            Log.e("Could not save", e.toString());
        }
    }

Thanks to appreciate.

Aucun commentaire:

Enregistrer un commentaire