Skip to content

Instantly share code, notes, and snippets.

@shumelchyk
Created July 17, 2016 13:21
Show Gist options
  • Save shumelchyk/f28d47f8e19bbf4a66781fdcb1f95b00 to your computer and use it in GitHub Desktop.
Save shumelchyk/f28d47f8e19bbf4a66781fdcb1f95b00 to your computer and use it in GitHub Desktop.
AssetExtractor code
package com.google.appinventor.components.runtime;
import android.content.res.AssetManager;
import android.os.Environment;
import com.google.appinventor.components.annotations.*;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.common.PropertyTypeConstants;
import com.google.appinventor.components.common.YaVersion;
import com.google.appinventor.components.runtime.util.FileUtil;
import java.io.File;
import java.io.*;
@DesignerComponent(version = YaVersion.ASSETS_EXTRACTOR_COMPONENT_VERSION,
description = "Non-visible component that extracts file from the assets to the storage " +
"files.",
category = ComponentCategory.STORAGE,
nonVisible = true,
iconName = "images/file.png")
@SimpleObject
@UsesPermissions(permissionNames = "android.permission.WRITE_EXTERNAL_STORAGE, android.permission.READ_EXTERNAL_STORAGE")
public class AssetExtractor extends AndroidNonvisibleComponent {
private String filePath = ""; // Picture property
public AssetExtractor(ComponentContainer container) {
super(container.$form());
}
/**
* Takes file from the assets folder and then asynchronously writes it
* to external storage. This allows to share files between apps.
*/
@SimpleFunction(description = "Takes file from the assets folder and then asynchronously writes it" +
"to external storage. This allows to share files between apps")
public void ExtractFile() {
FileUtil.checkExternalStorageWriteable();
copyAssets(filePath, filePath);
}
@DesignerProperty(editorType = PropertyTypeConstants.PROPERTY_TYPE_ASSET, defaultValue = "")
@SimpleProperty
public void File(String path) {
filePath = (path == null) ? "" : path;
}
/**
* Event indicating that a request has finished.
*
* @param fileUri read from the file
*/
@SimpleEvent(description = "Event indicating that the contents from the file have been copied to the file system.")
public void FileExtracted(String fileUri) {
EventDispatcher.dispatchEvent(this, "FileExtracted", fileUri);
}
private void copyAssets(String filePath, String outFileName) {
AssetManager assetManager = form.getBaseContext().getAssets();
InputStream in = null;
OutputStream out = null;
File outFile = null;
try {
in = assetManager.open(filePath);
outFile = new File(Environment.getExternalStorageDirectory(), outFileName);
out = new FileOutputStream(outFile);
copyFile(in, out);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
FileExtracted(outFile.getAbsolutePath());
}
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);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment