Trying to create a Flutter plugin that copies an asset file to the native Application Documents Folder.
For iOS, I achieved this by the following code (see below).
However, since I do not have much knowledge of the Android architecture, I would like to know how my Android MethodChannel code should look like.
My Android part of this Flutter plugin needs to be in KOTLIN !
I need a file copy from the Android assets folder to the Documents Folder of Android - all this done inside the Flutter plugin and in Kotlin!
Again, I have iOS in Swift ready made. What is missing is the Android in Kotlin counter part. Do you have any help on this ?
.
Here is the working code for the iOS FlutterMethodChannel in Swift:
(i.e. it copies a file from the main-bundle to the Documents-Directory of the iPhone...)
import UIKit
private func copyFile(fileName: String) -> String {
let fileManager = FileManager.default
let documentsUrl = fileManager.urls(for: .documentDirectory,
in: .userDomainMask)
guard documentsUrl.count != 0 else {
return "Could not find documents URL"
}
let finalURL = documentsUrl.first!.appendingPathComponent(fileName)
if !( (try? finalURL.checkResourceIsReachable()) ?? false) {
let documentsURL = Bundle.main.resourceURL?.appendingPathComponent(fileName)
do {
try fileManager.copyItem(atPath: (documentsURL?.path)!, toPath: finalURL.path)
return "\(finalURL.path)"
} catch let error as NSError {
return "Couldn't copy file to final location! Error:\(error.description)"
}
} else {
return "\(finalURL.path)"
}
}
In Kotlin, I tried this - but it does not work at all....:(
import java.io.File
private fun copyFileTrial1(fileName: String): String {
File src = new File("../../assets/${fileName}");
File dst = new File("../../DocumentsFolder/${fileName}", src.getName());
FileInputStream inStream = new FileInputStream(src);
FileOutputStream outStream = new FileOutputStream(dst);
FileChannel inChannel = inStream.getChannel();
FileChannel outChannel = outStream.getChannel();
inChannel.transferTo(0, inChannel.size(), outChannel);
inStream.close();
outStream.close();
return "hello1"
}
Or I tried this - but again - completely without success :(
private fun copyFileTrial2(fileName: String): String {
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open(fileName);
String outDir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/X/Y/Z/" ;
File outFile = new File(outDir, filenfileNameame);
out = new FileOutputStream(outFile);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch(IOException e) {
Log.e("tag", "Failed to copy asset file: " + fileName, e);
}
return "hello2"
}
private void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int read;
while((read = in.read(buffer)) != -1){
out.write(buffer, 0, read);
}
}
I have finally found a solution to file copy issue in Kotlin !
It was especially helpful in achieving my very first Flutter plugin.
Here is the solution of the file-copy in Kotlin
import java.io.File
import java.io.InputStream
import io.flutter.util.PathUtils
private fun copyFile(fileName: String): String {
val assetStream: InputStream = mRegistrar.context().assets.open(fileName)
val appliationDocumentsFolderPath: String = PathUtils.getDataDirectory(mRegistrar.context())
val outputFilePath: String = appliationDocumentsFolderPath + "/" + fileName
if (!File(outputFilePath).exists()) {
File(outputFilePath).copyInputStreamToFile(assetStream)
}
return outputFilePath
}
private fun File.copyInputStreamToFile(inputStream: InputStream) {
inputStream.use { input ->
this.outputStream().use { fileOut ->
input.copyTo(fileOut)
}
}
}
Related
I want to create a zip file in Xamarin Forms Cross Platform.
I use a custom way for every platform, iOS and Android.
In iOS works with the Library ZipArchive, but I not found alternative for Android.
So I try do it native (to create zip with only one file), but the zip file was created empty.
public void Compress(string path, string filename, string zipname)
{
var personalpath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
string folder = Path.Combine(personalpath, path);
string zippath = Path.Combine(folder, zipname);
string filepath = Path.Combine(folder, filename);
System.IO.FileStream fos = new System.IO.FileStream(zippath, FileMode.OpenOrCreate);
Java.Util.Zip.ZipOutputStream zos = new Java.Util.Zip.ZipOutputStream(fos);
ZipEntry entry = new ZipEntry(filename.Substring(filename.LastIndexOf("/") + 1));
byte[] fileContents = File.ReadAllBytes(filepath);
zos.Write(fileContents);
zos.CloseEntry();
}
Solution by Leo Nix and OP.
Need to close ZOS.
fos and zos should be disposed.
...
zos.CloseEntry();
zos.Close();
zos.Dispose();
fos.Dispose();
}
I've noticed the ask and solution code wasn't complete. I had to change somethings to make it work, so here is the complete code:
public void ZipFile(string fullZipFileName, params string[] fullFileName)
{
using (FileStream fs = new FileStream(fullZipFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
using (ZipOutputStream zs = new ZipOutputStream(fs))
{
foreach (var file in fullFileName)
{
string fileName = Path.GetFileName(file);
ZipEntry zipEntry = new ZipEntry(fileName);
zs.PutNextEntry(zipEntry);
byte[] fileContent = System.IO.File.ReadAllBytes(file);
zs.Write(fileContent);
zs.CloseEntry();
}
zs.Close();
}
fs.Close();
}
}
I hope it help.
I am making a soundboard for practice and I want to give the user the ability to download the sound (that I have included in the app in the res/raw folder) onClick of a menu item but I can only find information about downloading from an internet url, not something that I already included in the apk.
What is the best way to do this? I would like to give them the option to save to an SD card also if this is possible. A point towards the correct class to use in the documentation would be great! I've been googling to no avail.
Thanks!
Try something like this:
public void saveResourceToFile() {
InputStream in = null;
FileOutputStream fout = null;
try {
in = getResources().openRawResource(R.raw.test);
String downloadsDirectoryPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
String filename = "myfile.mp3"
fout = new FileOutputStream(new File(downloadsDirectoryPath + filename));
final byte data[] = new byte[1024];
int count;
while ((count = in.read(data, 0, 1024)) != -1) {
fout.write(data, 0, count);
}
} finally {
if (in != null) {
in.close();
}
if (fout != null) {
fout.close();
}
}
}
I don't know about the raw but I did a similar thing in my app using the assets folder. My files are under the assets/backgrounds folder as you can probably guess from the code below.
You can modify this code and make it work for you (I know I will only have 4 files which is why I have i go from 0 to 4 but you can change this to whatever you want).
This code copies the file starting with prefix_ (like prefix_1.png, prefix_2.png, etc) to my cache directory but you can obviously change the extension, the filename or the path you would like to save the assets to.
public static void copyAssets(final Context context, final String prefix) {
for (Integer i = 0; i < 4; i++) {
String filename = prefix + "_" + i.toString() + ".png";
File f = new File(context.getCacheDir() + "/" + filename);
if (f.exists()) {
f.delete();
}
if (!f.exists())
try {
InputStream is = context.getAssets().open("backgrounds/" + filename);
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
FileOutputStream fos = new FileOutputStream(f);
fos.write(buffer);
fos.close();
} catch (Exception e) {
Log.e("Exception occurred while trying to load file from assets.", e.getMessage());
}
}
}
I'm writing app for android and I'm using caffe library. My problem is that on start I need to initialize caffe, which is done by passing two files (structures of network) to caffe.
Problem is that I don't know how to store extra files on device. I've added model file to assets, but I don't know how can I read it using file path. Can you tell me where to store these file that could be access using file path?
Thanks for any ideas.
This should do it. Just copy those files to data directory from asset folder. If you already have those files there just load them.
String toPath = "/data/data/" + getPackageName(); // Your application path
private static boolean copyAssetFolder(AssetManager assetManager,
String fromAssetPath, String toPath) {
try {
String[] files = assetManager.list(fromAssetPath);
new File(toPath).mkdirs();
boolean res = true;
for (String file : files)
if (file.contains("."))
res &= copyAsset(assetManager,
fromAssetPath + "/" + file,
toPath + "/" + file);
else
res &= copyAssetFolder(assetManager,
fromAssetPath + "/" + file,
toPath + "/" + file);
return res;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
private static boolean copyAsset(AssetManager assetManager,
String fromAssetPath, String toPath) {
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open(fromAssetPath);
new File(toPath).createNewFile();
out = new FileOutputStream(toPath);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
return true;
} catch(Exception e) {
e.printStackTrace();
return false;
}
}
private static void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int read;
while((read = in.read(buffer)) != -1){
out.write(buffer, 0, read);
}
}
Put them into your project as assets, and then when the app starts, you can read them from the assets and copy them into the app's private storage area. You can find this directory using Context.getFilesDir().
From there, you'll be able to pass the files to Caffe.
Assets are packaged and access only using special methods, so i solved problem by access file and then copy it to new location which i passed to native method.
I'm using pliablematter simple-cloud-storage to manage uploads/downloads of files using Google Cloud Storage. But I'm not able to make it work, there's a property file with this content:
project.id=0000000000
application.name=Application Name
account.id=0000000000#developer.gserviceaccount.com
private.key.path=/var/key
I know my project.id, aplication name and account id but what should I put in private key path?? I generated and downloaded the service account private key but no matter which path location I always get java.io.FileNotFoundException
Moreover, where should I save private keys in Android applications?
Github project https://github.com/pliablematter/simple-cloud-storage
Please help! Thanks
I was able to solve this by copying the private key to an internal storage folder, then I put the path location in private.key.path
Don't know if this is the right way but it worked for me.
Here are the changed which you need to make after using that example to make it functional in android.
Download the private_key.p12 file from Google Console and put in Assets.
Save your cloudstorage.properties file in Assets as well.
Now make these method changes in CloudStorage class.
private Properties getProperties() throws Exception {
if (properties == null) {
properties = new Properties();
AssetManager manager = context.getAssets();
InputStream stream = manager.open("cloudstorage.properties");
try {
properties.load(stream);
} catch (IOException
e) {
throw new RuntimeException(
"cloudstorage.properties must be present in classpath",
e);
} finally {
if (stream != null)
stream.close();
}
}
return properties;
}
private Storage getStorage() throws Exception {
if (storage == null) {
HttpTransport httpTransport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
List<String> scopes = new ArrayList<>();
scopes.add(StorageScopes.DEVSTORAGE_FULL_CONTROL);
Credential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setServiceAccountId(
getProperties().getProperty(ACCOUNT_ID_PROPERTY))
.setServiceAccountPrivateKeyFromP12File(getPrivateKeyFile())
.setServiceAccountScopes(scopes).build();
storage = new Storage.Builder(httpTransport, jsonFactory,
credential).setApplicationName(
getProperties().getProperty(APPLICATION_NAME_PROPERTY))
.build();
}
return storage;
}
private File getPrivateKeyFile() {
File f = new File(context.getCacheDir() + “/my_private_key.p12");
if (!f.exists())
try {
InputStream is = context.getAssets().open(“private_key.p12");
FileOutputStream fos = new FileOutputStream(f);
byte[] buffer = new byte[4 * 1024];
int read;
while ((read = is.read(buffer)) != -1)
fos.write(buffer, 0, read);
fos.flush();
is.close();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
Log.e("FILE", "FETCHED FILE:: " + f.getAbsolutePath() + " with data: " + f.length());
return f;
}
I am using the following code to unzip a set of files (containing folders as well):
private boolean unpackZip(String path, String zipname)
{
InputStream is;
ZipInputStream zis;
try
{
String filename;
is = new FileInputStream(path + zipname);
zis = new ZipInputStream(new BufferedInputStream(is));
ZipEntry ze;
byte[] buffer = new byte[1024];
int count;
while ((ze = zis.getNextEntry()) != null)
{
// zapis do souboru
filename = ze.getName();
// Need to create directories if not exists, or
// it will generate an Exception...
if (ze.isDirectory()) {
File fmd = new File(path + filename);
fmd.mkdirs();
continue;
}
FileOutputStream fout = new FileOutputStream(path + filename);
// cteni zipu a zapis
while ((count = zis.read(buffer)) != -1)
{
fout.write(buffer, 0, count);
}
fout.close();
zis.closeEntry();
}
zis.close();
}
catch(IOException e)
{
e.printStackTrace();
return false;
}
return true;
}
The code fails on FileOutputStream fout = new FileOutputStream(path + filename) with the error:
java.io.FileNotFoundException: /storage/emulated/0/BASEFOLDER/FOLDER1/FILE.png
BASEFOLDER already exists, that is where I am trying to unzip the folder to. If I manually (or programmatically) create FOLDER1, the code runs fine and successfully unzips. I believe it is crashing because the very first file (ze) is named FOLDER1/FILE.png and FOLDER1 hasn't been created yet. How do I get around this? I know other people have used this code, I find it unlikely that it randomly doesn't work for me...
Have you got this in your AndroidManifest.xml file?
Add Write to external Storage permission
uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
I had the same problem. After several investigation I found that. put following single line in your code:
if (ze.isDirectory()) {
File fmd = new File(path + filename);
fmd.mkdirs();
zis.closeEntry(); // <<<<<< ADD THIS LINE
continue;
}
Sometime the extract files has been extracted before its parent directory is created, for example:
File A inside directory B. But B directory is not created, index of files listing below cause the issue:
dir_b/file_a.txt
dir_b/
dir_b/file_c.txt
So, to sure directory created before file extracting, you need to create parent directories first, for example:
val targetFile = File(tempOutputDir, zipEntry.name)
if (zipEntry.isDirectory) {
targetFile.mkdirs()
} else {
try {
try {
targetFile.parentFile?.mkdirs() // <-- ADD THIS LINE
} catch (exception: Exception) {
Log.e("ExampleApp", exception.localizedMessage, exception)
}
val bufferOutputStream = BufferedOutputStream(
FileOutputStream(targetFile)
)
val buffer = ByteArray(1024)
var read = zipInputStream.read(buffer)
while (read != -1) {
bufferOutputStream.write(buffer, 0, read)
read = zipInputStream.read(buffer)
}
bufferOutputStream.close()
} catch (exception: Exception) {
Log.e("ExampleApp", exception.localizedMessage, exception)
}
}