This repository has been archived by the owner on Aug 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[android] copy files from assets task util
- Loading branch information
1 parent
4004a85
commit 1c6c7d5
Showing
3 changed files
with
115 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
...d/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/utils/FileUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package com.mapbox.mapboxsdk.testapp.utils | ||
|
||
import android.content.Context | ||
import android.os.AsyncTask | ||
import java.io.File | ||
import java.io.FileOutputStream | ||
import java.lang.ref.WeakReference | ||
|
||
class FileUtils { | ||
|
||
/** | ||
* Task that copies a file from the assets directory to a provided directory. | ||
* The asset's name is going to be kept in the new directory. | ||
*/ | ||
class CopyFileFromAssetsTask(context: Context, listener: OnFileCopiedFromAssetsListener) : AsyncTask<String, Void, Boolean>() { | ||
private val contextWeakReference: WeakReference<Context> = WeakReference(context) | ||
private val listenerWeakReference: WeakReference<OnFileCopiedFromAssetsListener> = WeakReference(listener) | ||
|
||
override fun doInBackground(vararg strings: String): Boolean? { | ||
val assetName = strings[0] | ||
val destinationPath = strings[1] | ||
|
||
contextWeakReference.get()?.let { | ||
try { | ||
copyAsset(it, assetName, destinationPath) | ||
} catch (ex: Exception) { | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} | ||
|
||
override fun onCancelled() { | ||
listenerWeakReference.get()?.onError() | ||
} | ||
|
||
override fun onPostExecute(result: Boolean) { | ||
if (result) { | ||
listenerWeakReference.get()?.onFileCopiedFromAssets() | ||
} else { | ||
listenerWeakReference.get()?.onError() | ||
} | ||
} | ||
|
||
private fun copyAsset(context: Context, assetName: String, destinationPath: String) { | ||
val bufferSize = 1024 | ||
val assetManager = context.assets | ||
val inputStream = assetManager.open(assetName) | ||
val outputStream = FileOutputStream(File(destinationPath, assetName)) | ||
try { | ||
inputStream.copyTo(outputStream, bufferSize) | ||
} finally { | ||
inputStream.close() | ||
outputStream.flush() | ||
outputStream.close() | ||
} | ||
} | ||
} | ||
|
||
interface OnFileCopiedFromAssetsListener { | ||
fun onFileCopiedFromAssets() | ||
fun onError() | ||
} | ||
} |