0

I need to get the file URL to an image that I store locally in my app. I don't care where I store the file locally, just somewhere in which I can get a URL to it. I have tried assets and resources with no luck.

What I am trying to do is override UrlTileProvider:

public class FileSystemTileProvider extends UrlTileProvider {

    public FileSystemTileProvider(int width, int height, String assestsDirectory) {
        super(width, height);
    }

    @Override
    public URL getTileUrl(int x, int y, int z) {
        String tile = "file://<somewhere>/background.png";

        URL fileUrl = null;
        try {
            fileUrl = new URL(tile);
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        return fileUrl;
    }
}

I need to return a URL for this to work. I have hardcoded a file url and it does work. However I need a way to get a URL for my background.png image. Is this possible?

lostintranslation
  • 23,756
  • 50
  • 159
  • 262
  • I think the file url has 3 slashes file:/// in phonegap you can use something like file:///android_asset/www/index.html – Carlos487 Jul 01 '14 at 21:57

2 Answers2

2

The easiest way would probably be to include the file as an asset in the APK, then extract it to the app's private directory with an AssetManager. For example as explained in this answer, except using getFilesDir() instead of getExternalFilesDir() (or even easier, by creating the FileOutputStream object with openFileOutput()).

Another option (especially if you want/need to provide different background images for different display densities) is to include it as a drawable, then extract it with a similar method.

After you have done either of these (only once, say at the main activity's start-up) and saved the file with a known name, just create the URL to this file.

File backgroundFile = new File(getFilesDir(), fileName);
URL fileUrl = backgroundFile.toURI().toURL();
Community
  • 1
  • 1
matiash
  • 54,791
  • 16
  • 125
  • 154
1

I don't think you can access it that way.

But you can instead implement directly TileProvider and write your own implementation of

public Tile getTile (int x, int y, int zoom) {
   //get the drawable
   ...
   Bitmap bitmap = drawable.getBitmap();
   ByteArrayOutputStream stream = new ByteArrayOutputStream();
   bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
   return Tile(WIDTH, HEIGHT, stream.toByteArray());
}

Get the drawable from the drawables folder with:

Resources res = getResources();
Drawable shape = res. getDrawable(R.drawable.background);

or from assets folder with:

InputStream ins = getAssets().open("background.jpg");
Drawable d = Drawable.createFromStream(ins, null);
Daniel Alexandrov
  • 1,299
  • 8
  • 13
  • Exactly what I was looking for. I got stuck I trying to do a UrlTileProvider, not sure why I got that stuck in my head an could not get around it. Thanks. – lostintranslation Jul 03 '14 at 16:29