Skip to content

Instantly share code, notes, and snippets.

@johnolafenwa
Created October 16, 2019 22:59
Show Gist options
  • Select an option

  • Save johnolafenwa/c80b194dafd0c56c1b4f25c5ffe63e20 to your computer and use it in GitHub Desktop.

Select an option

Save johnolafenwa/c80b194dafd0c56c1b4f25c5ffe63e20 to your computer and use it in GitHub Desktop.

Revisions

  1. johnolafenwa created this gist Oct 16, 2019.
    33 changes: 33 additions & 0 deletions Utils.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    package com.johnolafenwa.pytorchandroid;

    import android.content.Context;
    import android.util.Log;

    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;

    public class Utils {

    public static String assetFilePath(Context context, String assetName) {
    File file = new File(context.getFilesDir(), assetName);

    try (InputStream is = context.getAssets().open(assetName)) {
    try (OutputStream os = new FileOutputStream(file)) {
    byte[] buffer = new byte[4 * 1024];
    int read;
    while ((read = is.read(buffer)) != -1) {
    os.write(buffer, 0, read);
    }
    os.flush();
    }
    return file.getAbsolutePath();
    } catch (IOException e) {
    Log.e("pytorchandroid", "Error process asset " + assetName + " to file path");
    }
    return null;
    }

    }