修复了小地图自定义样式没有生效的bug,原因是高德地图的问题,解决方案如下

aMap.setOnMapLoadedListener(new AMap.OnMapLoadedListener() {

            @Override
            public void onMapLoaded() {
                //Log.w("onMapLoaded", "===onMapLoaded====");
                // 高德地图有bug,所以需要多次调用设置皮肤才能成功
                if (options != null) {
                    options.setCustomMapStylePath(styleFilePath);
                    mAMapNaviView.setViewOptions(options);
                }

                //设置希望展示的地图缩放级别
                CameraUpdate cameraUpdate = CameraUpdateFactory.zoomTo(12);
                aMap.moveCamera(cameraUpdate);
            }
        });
This commit is contained in:
董宏宇
2020-12-22 13:00:48 +08:00
parent eff36419b4
commit 0b8c5205ef
5 changed files with 319 additions and 111 deletions

View File

@@ -5,7 +5,9 @@ import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;
import androidx.annotation.IntRange;
import androidx.core.content.FileProvider;
import java.io.BufferedInputStream;
@@ -20,98 +22,100 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
/**
* 文件工具类
*/
public class FileUtils {
public static boolean createFileDir( String fileDir ) {
public static boolean createFileDir(String fileDir) {
if ( TextUtils.isEmpty( fileDir ) ) {
if (TextUtils.isEmpty(fileDir)) {
return false;
}
try {
File dir = new File( fileDir );
File dir = new File(fileDir);
return dir.exists() || dir.mkdir();
} catch ( Exception e ) {
} catch (Exception e) {
return false;
}
}
public static boolean createFileDir( File dir ) {
if ( dir == null ) {
public static boolean createFileDir(File dir) {
if (dir == null) {
return false;
}
try {
return dir.exists() || dir.mkdir();
} catch ( Exception e ) {
} catch (Exception e) {
return false;
}
}
public static void writeToFile( String fileDir, String fileName, String content ) {
if ( fileDir == null || fileName == null || content == null ) {
public static void writeToFile(String fileDir, String fileName, String content) {
if (fileDir == null || fileName == null || content == null) {
return;
}
if ( !createFileDir( fileDir ) ) {
if (!createFileDir(fileDir)) {
return;
}
FileOutputStream fos = null;
OutputStreamWriter osw = null;
try {
fos = new FileOutputStream( fileDir + fileName, true );
osw = new OutputStreamWriter( fos );
osw.write( content );
fos = new FileOutputStream(fileDir + fileName, true);
osw = new OutputStreamWriter(fos);
osw.write(content);
osw.flush();
} catch ( IOException e ) {
} catch (IOException e) {
e.printStackTrace();
} finally {
IOUtils.closeSilently( fos );
IOUtils.closeSilently( osw );
IOUtils.closeSilently(fos);
IOUtils.closeSilently(osw);
}
}
/**
* Read a text file into a String, optionally limiting the length.
*/
public static String readTextFile( File file ) {
public static String readTextFile(File file) {
InputStream is = null;
BufferedInputStream bis = null;
ByteArrayOutputStream bos = null;
String text = null;
try {
is = new FileInputStream( file );
bis = new BufferedInputStream( is );
is = new FileInputStream(file);
bis = new BufferedInputStream(is);
bos = new ByteArrayOutputStream();
int len;
byte[] data = new byte[1024];
do {
len = bis.read( data );
if ( len > 0 ) bos.write( data, 0, len );
} while ( len == data.length );
len = bis.read(data);
if (len > 0) bos.write(data, 0, len);
} while (len == data.length);
text = bos.toString();
} catch ( Exception e ) {
} catch (Exception e) {
e.printStackTrace();
} finally {
IOUtils.closeSilently( is );
IOUtils.closeSilently( bis );
IOUtils.closeSilently( bos );
IOUtils.closeSilently(is);
IOUtils.closeSilently(bis);
IOUtils.closeSilently(bos);
}
return text;
}
public static String fileToBase64( File file ) {
public static String fileToBase64(File file) {
String base64 = null;
InputStream in = null;
try {
in = new FileInputStream( file );
in = new FileInputStream(file);
byte[] bytes = new byte[in.available()];
int length = in.read( bytes );
base64 = Base64.encodeToString( bytes, 0, length, Base64.DEFAULT );
} catch ( FileNotFoundException e ) {
int length = in.read(bytes);
base64 = Base64.encodeToString(bytes, 0, length, Base64.DEFAULT);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch ( IOException e ) {
} catch (IOException e) {
e.printStackTrace();
} finally {
IOUtils.closeSilently( in );
IOUtils.closeSilently(in);
}
return base64;
}
@@ -119,81 +123,81 @@ public class FileUtils {
/**
* Writes string to file. Basically same as "echo -n $string > $filename"
*/
public static void stringToFile( String filename, String string ) {
public static void stringToFile(String filename, String string) {
FileWriter out = null;
try {
out = new FileWriter( filename );
out.write( string );
} catch ( Exception e ) {
out = new FileWriter(filename);
out.write(string);
} catch (Exception e) {
e.printStackTrace();
} finally {
IOUtils.closeSilently( out );
IOUtils.closeSilently(out);
}
}
public static InputStream stringToStream( String content ) {
public static InputStream stringToStream(String content) {
InputStream inputStream = null;
try {
inputStream = new ByteArrayInputStream( content.getBytes() );
} catch ( Exception e ) {
inputStream = new ByteArrayInputStream(content.getBytes());
} catch (Exception e) {
e.printStackTrace();
}
return inputStream;
}
public static String streamToString( InputStream is ) throws IOException {
public static String streamToString(InputStream is) throws IOException {
String content = null;
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int i = -1;
while ( ( i = is.read() ) != -1 ) {
bos.write( i );
while ((i = is.read()) != -1) {
bos.write(i);
}
content = bos.toString();
} catch ( Exception e ) {
} catch (Exception e) {
e.printStackTrace();
}
return content;
}
public static String getStringFromFile( Context context, String fileName ) {
public static String getStringFromFile(Context context, String fileName) {
FileInputStream fis = null;
ByteArrayOutputStream os = null;
String content = null;
try {
fis = context.openFileInput( fileName );
fis = context.openFileInput(fileName);
os = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length = -1;
while ( ( length = fis.read( buffer ) ) != -1 ) {
os.write( buffer, 0, length );
while ((length = fis.read(buffer)) != -1) {
os.write(buffer, 0, length);
}
content = os.toString();
} catch ( Exception e ) {
} catch (Exception e) {
e.printStackTrace();
} finally {
IOUtils.closeSilently( fis );
IOUtils.closeSilently( os );
IOUtils.closeSilently(fis);
IOUtils.closeSilently(os);
}
return content;
}
public static InputStream getStreamFromFile( Context context, String fileName ) {
public static InputStream getStreamFromFile(Context context, String fileName) {
FileInputStream fis = null;
try {
fis = context.openFileInput( fileName );
} catch ( Exception e ) {
fis = context.openFileInput(fileName);
} catch (Exception e) {
e.printStackTrace();
}
return fis;
}
public static void saveStringToFile( Context context, String content, String fileName ) {
public static void saveStringToFile(Context context, String content, String fileName) {
try {
FileOutputStream fos = context.openFileOutput( fileName, Context.MODE_PRIVATE );
fos.write( content.getBytes() );
IOUtils.closeSilently( fos );
} catch ( Exception e ) {
FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
fos.write(content.getBytes());
IOUtils.closeSilently(fos);
} catch (Exception e) {
e.printStackTrace();
}
}
@@ -201,10 +205,10 @@ public class FileUtils {
/**
* 将scheme为file的uri转成FileProvider 提供的content uri
*/
public static Uri convertFileUriToFileProviderUri( Context context, Uri uri ) {
if ( uri == null ) return null;
if ( ContentResolver.SCHEME_FILE.equals( uri.getScheme() ) ) {
return getUriForFile( context, new File( uri.getPath() ) );
public static Uri convertFileUriToFileProviderUri(Context context, Uri uri) {
if (uri == null) return null;
if (ContentResolver.SCHEME_FILE.equals(uri.getScheme())) {
return getUriForFile(context, new File(uri.getPath()));
}
return uri;
@@ -213,38 +217,195 @@ public class FileUtils {
/**
* 创建一个用于拍照图片输出路径的Uri (FileProvider)
*/
public static Uri getUriForFile( Context context, File file ) {
return FileProvider.getUriForFile( context, getFileProviderName( context ), file );
public static Uri getUriForFile(Context context, File file) {
return FileProvider.getUriForFile(context, getFileProviderName(context), file);
}
public static String getFileProviderName( Context context ) {
public static String getFileProviderName(Context context) {
return context.getPackageName() + ".fileprovider";
}
/**
* 把Uri 解析出文件绝对路径
*/
public static String parseOwnUri( Context context, Uri uri ) {
if ( uri == null ) return null;
public static String parseOwnUri(Context context, Uri uri) {
if (uri == null) return null;
String path;
if ( TextUtils.equals( uri.getAuthority(), getFileProviderName( context ) ) ) {
path = new File( uri.getPath() ).getAbsolutePath();
if (TextUtils.equals(uri.getAuthority(), getFileProviderName(context))) {
path = new File(uri.getPath()).getAbsolutePath();
} else {
path = uri.getPath();
}
return path;
}
public static String getFileStreamPath( Context context, String name ) {
public static String getFileStreamPath(Context context, String name) {
String absFileName = null;
try {
File file = context.getFileStreamPath( name );
if ( file != null && file.exists() ) {
File file = context.getFileStreamPath(name);
if (file != null && file.exists()) {
absFileName = file.getAbsolutePath();
}
} catch ( Exception e ) {
} catch (Exception e) {
e.printStackTrace();
}
return absFileName;
}
/**
* 拷贝文件数据到指定目录
*
* @param is
* @param to
* @param listener
*/
public static void copy(final InputStream is, final String to, final FileCopyListener listener) {
new Thread(new Runnable() {
@Override
public void run() {
Log.w("FileUtils", "======copy======");
if (listener != null) {
listener.onStart();
}
try {
long fileSize = is.available();
long process = 0;
byte[] buff = new byte[1024];
int rc = 0;
File toFile = new File(to);
if (!toFile.getParentFile().exists()) {
toFile.getParentFile().mkdirs();
}
FileOutputStream fos = new FileOutputStream(toFile);
while ((rc = is.read(buff, 0, 1024)) > 0) {
process += rc;
fos.write(buff, 0, rc);
if (listener != null) {
listener.onProcess(((int) (((float) process) * 100 / fileSize)));
}
}
fos.flush();
fos.close();
is.close();
} catch (Exception e) {
if (listener != null) {
listener.onFail(e);
return;
}
}
if (listener != null) {
listener.onFinish(to);
}
}
}).start();
}
/**
* 拷贝文件到制定目录
*
* @param from
* @param to
* @param listener
*/
public static void copy(final String from, final String to, final FileCopyListener listener) {
new Thread(new Runnable() {
@Override
public void run() {
File file = null;
try {
file = new File(from);
} catch (Exception e) {
if (listener != null) {
listener.onFail(e);
}
return;
}
if (!file.isFile()) {
if (listener != null) {
listener.onFail(new Exception(String.format("%s is not a file", from)));
return;
}
}
if (!file.exists()) {
if (listener != null) {
listener.onFail(new FileNotFoundException(String.format("%s is not exists.", from)));
return;
}
}
if (listener != null) {
listener.onStart();
}
long fileSize = file.length();
long process = 0;
try {
FileInputStream fis = new FileInputStream(file);
byte[] buff = new byte[1024];
int rc = 0;
File toFile = new File(to);
if (!toFile.getParentFile().exists()) {
toFile.getParentFile().mkdirs();
}
FileOutputStream fos = new FileOutputStream(toFile);
while ((rc = fis.read(buff, 0, 1024)) > 0) {
process += rc;
fos.write(buff, 0, rc);
if (listener != null) {
listener.onProcess(((int) (((float) process) * 100 / fileSize)));
}
}
fos.flush();
fos.close();
fis.close();
} catch (Exception e) {
if (listener != null) {
listener.onFail(e);
return;
}
}
if (listener != null) {
listener.onFinish(to);
}
}
}).start();
}
/**
* 文件拷贝监听
*/
public interface FileCopyListener {
// 开始
void onStart();
// 失败
void onFail(Exception e);
// 进度
void onProcess(@IntRange(from = 0, to = 100) int process);
// 结束成功
void onFinish(String toPath);
}
}