moagrius / TileView

TileView is a subclass of android.view.ViewGroup that asynchronously displays, pans and zooms tile-based images. Plugins are available for features like markers, hotspots, and path drawing.
MIT License
1.46k stars 337 forks source link

Alternative tile sources #508

Closed ghost closed 5 years ago

ghost commented 5 years ago

Greetings Where can i find information on how to use alternative tile sources like SD card? This wiki article: https://github.com/moagrius/TileView/wiki/Tile-Sources is no longer exists. Any information will be usefull, thanks. (using 2.2.9 version)

moagrius commented 5 years ago

You'd just make a StreamProvider that grabbed the file from the SD card and returned a FileInputStream

ghost commented 5 years ago

It took me a while to figure out how it needs to be done and I found its a common question here so Ill leave it here for dummies like me To load tiles from external storage you need to create a class that implements BitmapProvider interface and define getBitmap method that returns Bitmap

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import com.qozix.tileview.graphics.BitmapProvider;
import com.qozix.tileview.tiles.Tile;

public class BitmapProviderExternal implements BitmapProvider{
    public Bitmap getBitmap(Tile tile, Context context ){
        Object data = tile.getData();
        if(data instanceof String){
            String unformattedFileName = (String) tile.getData();
            String formattedFileName = String.format(unformattedFileName, tile.getColumn(), tile.getRow());
            BitmapFactory.Options bmOptions = new BitmapFactory.Options();
            Bitmap bitmap = BitmapFactory.decodeFile(formattedFileName,bmOptions);
            return bitmap;
        }
        return null;
    }
}

and then set BitmapProvider to this class tileView.setBitmapProvider( new BitmapProviderExternal()); Find directory that you're using to store your files (in my case its folder in documents called Tiles) File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS); dir = new File(dir.getAbsolutePath()+ "/Tiles"); and then just use path to your file tileView.addDetailLevel( 1f, dir.getAbsolutePath() + "/tile-%d_%d.png", 256, 256);