1
votes

I have developed one game, it has more images so I am thinking load images from server for every game level complete in my phonegap game. I need to download the images from server to this location

file:///android_asset/www/img in phonegap game.

how can I achieve this?

I have gone through the tutorial Code for displaying image in phonegap but here they have displayed images on the screen, I dont to save them in img folder..

function storeIntelligrapeLogo() { //alert('start of file download'); var url = "http://androidexample.com/media/webservice/LazyListView_images/image0.png"; // image url window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fs) { //alert('inside request file sysytem') // fs.root.getDirectory(myFolderName, {create:true, exclusive:false}, function (folder){});

      var imagePath = 'file:///storage/sdcard0'+fs.root.fullPath + "/logo2.png"; // full file path
      // var imagePath = 'file:///android_asset/www/'+fs.root.fullPath + "logo2.png";

       // alert('inside request file sysytem path fs.root==' + fs.root);
       // alert('inside request file sysytem path fs.root.fullPath==' + fs.root.fullPath);
        var fileTransfer = new FileTransfer();
        fileTransfer.download(url, imagePath, function(entry) {
           // alert(entry.fullPath); // entry is fileEntry object
            var dwnldImg = document.getElementById("dwnldImg");
            dwnldImg.src = imagePath;
            dwnldImg.style.visibility = "visible";
            dwnldImg.style.display = "block";

        }, function(error) {
            alert("Some error" + JSON.stringify(error));
        });
    }) } 

I have tried this example but its storing image in the phone memeory but I need to store images into phonegap img folder

Thanks in advance

1
send image as String Base64 from server and convert it to file in androidRandyka Yudhistira

1 Answers

-1
votes

Use this code :

/**
 * Load an image from internet and save it under the default repertory of the application
 */
public void downloadImageFromInternet(String url, String name)
{
    ImageLoader.getInstance().loadImage(url, 
        new SimpleImageLoadingListener()
        {
            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage)
            {
                OutputStream fOut;

                // You need to reference your context here. Depending on the class you inherite, "this", "getContext()" or "getActivity()" could work 
                File file = new File(context.getFilesDir(), name + ".jpeg");

                try
                {
                    fOut = new FileOutputStream(file);

                    loadedImage.compress(Bitmap.CompressFormat.JPEG, 90, fOut);

                    fOut.flush();
                    fOut.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        });
}

Read the File documentation to understand how to set the path of the saved file. In this code, the file will be saved inside the default folder of your application.

The library you need to download images from the web is Universal Image Loader.