android.content.Context.getAssets()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(10.1k)|赞(0)|评价(0)|浏览(319)

本文整理了Java中android.content.Context.getAssets()方法的一些代码示例,展示了Context.getAssets()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Context.getAssets()方法的具体详情如下:
包路径:android.content.Context
类名称:Context
方法名:getAssets

Context.getAssets介绍

暂无

代码示例

代码示例来源:origin: MindorksOpenSource/android-mvp-architecture

public static String loadJSONFromAsset(Context context, String jsonFileName)
    throws IOException {
  AssetManager manager = context.getAssets();
  InputStream is = manager.open(jsonFileName);
  int size = is.available();
  byte[] buffer = new byte[size];
  is.read(buffer);
  is.close();
  return new String(buffer, "UTF-8");
}

代码示例来源:origin: stackoverflow.com

private static void copyFile(String assetPath, String localPath, Context context) {
  try {
    InputStream in = context.getAssets().open(assetPath);
    FileOutputStream out = new FileOutputStream(localPath);
    int read;
    byte[] buffer = new byte[4096];
    while ((read = in.read(buffer)) > 0) {
      out.write(buffer, 0, read);
    }
    out.close();
    in.close();

  } catch (IOException e) {
    throw new RuntimeException(e);
  }
}

代码示例来源:origin: stackoverflow.com

File Path = Ctxt.getDir("Data", 0);
File DBFile = new File(Path, "database.db");

if(!DBFile.exists() || DatabaseNeedsUpgrade)  //Need to copy...
  CopyDatabase(Ctxt, DBFile);

static private void CopyDatabase(Context Ctxt, File DBFile) throws IOException
{
  AssetManager assets = Ctxt.getAssets();
  OutputStream outstream = new FileOutputStream(DBFile);
  DBFile.createNewFile();
  byte []b = new byte[1024];
  int i, r;
  String []assetfiles = assets.list("");
  Arrays.sort(assetfiles);
  for(i=1;i<10;i++) //I have definitely less than 10 files; you might have more
  {
    String partname = String.format("%d.db", i);
    if(Arrays.binarySearch(assetfiles, partname) < 0) //No such file in assets - time to quit the loop
      break;
    InputStream instream = assets.open(partname);
    while((r = instream.read(b)) != -1)
      outstream.write(b, 0, r);
    instream.close();
  }
  outstream.close();
}

代码示例来源:origin: dodola/RocooFix

public static String copyAsset(Context context, String assetName, File dir) throws IOException {
  File outFile = new File(dir, assetName);
  if (!outFile.exists()) {
    AssetManager assetManager = context.getAssets();
    InputStream in = assetManager.open(assetName);
    OutputStream out = new FileOutputStream(outFile);
    copyFile(in, out);
    in.close();
    out.close();
  }
  return outFile.getAbsolutePath();
}

代码示例来源:origin: sirmordred/AngelaRoot

private void copyFromAsset(Context ct, String fileName, String targetPath) {
  try (InputStream in = ct.getAssets().open(fileName);
     OutputStream out = new FileOutputStream(targetPath)) {
    byte[] buffer = new byte[1024];
    int read;
    while((read = in.read(buffer)) != -1) {
      out.write(buffer, 0, read);
    }
  } catch(IOException e) {
    Log.e("tag", "Failed to copy asset file: ", e);
  }
}

代码示例来源:origin: stackoverflow.com

public static String getXMLFromAssets(Context ctx, String pathToXML){   
    InputStream rawInput;
    //create a output stream to write the buffer into  
    ByteArrayOutputStream rawOutput = null;
    try {
      rawInput = ctx.getAssets().open(pathToXML);
      //create a buffer that has the same size as the InputStream  
      byte[] buffer = new byte[rawInput.available()];  
      //read the text file as a stream, into the buffer  
      rawInput.read(buffer);  
      rawOutput = new ByteArrayOutputStream();  
      //write this buffer to the output stream  
      rawOutput.write(buffer);  
      //Close the Input and Output streams  
      rawOutput.close();  
      rawInput.close();
    } catch (IOException e) {
      Log.e("Error", e.toString());
    }
    //return the output stream as a String  
    return rawOutput.toString();  
}

代码示例来源:origin: lzyzsd/AndroidHotFixExamples

public static void copyDex(Context context, String dexName) {
  File dexInternalStoragePath = new File(context.getDir("dex", Context.MODE_PRIVATE),
      dexName);
  BufferedInputStream bis = null;
  OutputStream dexWriter = null;
  try {
    bis = new BufferedInputStream(context.getAssets().open(dexName));
    dexWriter = new BufferedOutputStream(
        new FileOutputStream(dexInternalStoragePath));
    byte[] buf = new byte[BUF_SIZE];
    int len;
    while((len = bis.read(buf, 0, BUF_SIZE)) > 0) {
      dexWriter.write(buf, 0, len);
    }
    dexWriter.close();
    bis.close();
  } catch (FileNotFoundException e) {
    e.printStackTrace();
  } catch (IOException e) {
    e.printStackTrace();
  }
}

代码示例来源:origin: tiann/understand-plugin-framework

/**
 * 把Assets里面得文件复制到 /data/data/files 目录下
 *
 * @param context
 * @param sourceName
 */
public static void extractAssets(Context context, String sourceName) {
  AssetManager am = context.getAssets();
  InputStream is = null;
  FileOutputStream fos = null;
  try {
    is = am.open(sourceName);
    File extractFile = context.getFileStreamPath(sourceName);
    fos = new FileOutputStream(extractFile);
    byte[] buffer = new byte[1024];
    int count = 0;
    while ((count = is.read(buffer)) > 0) {
      fos.write(buffer, 0, count);
    }
    fos.flush();
  } catch (IOException e) {
    e.printStackTrace();
  } finally {
    closeSilently(is);
    closeSilently(fos);
  }
}

代码示例来源:origin: stackoverflow.com

InputStream mInput = mContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream mOutput = new FileOutputStream(outFileName);
byte[] mBuffer = new byte[1024];
int mLength;
while ((mLength = mInput.read(mBuffer))>0)
  mOutput.write(mBuffer, 0, mLength);
mOutput.flush();
mOutput.close();
mInput.close();

代码示例来源:origin: arimorty/floatingsearchview

private static String loadJson(Context context) {
  String jsonString;
  try {
    InputStream is = context.getAssets().open(COLORS_FILE_NAME);
    int size = is.available();
    byte[] buffer = new byte[size];
    is.read(buffer);
    is.close();
    jsonString = new String(buffer, "UTF-8");
  } catch (IOException ex) {
    ex.printStackTrace();
    return null;
  }
  return jsonString;
}

代码示例来源:origin: TommyLemon/APIJSON

private static CityDB openCityDB(Context context, String packageName) {
  String path = "/data"
      + Environment.getDataDirectory().getAbsolutePath()
      + File.separator + packageName + File.separator
      + CityDB.CITY_DB_NAME;
  File db = new File(path);
  if (!db.exists()) {
    try {
      InputStream is = context.getAssets().open("city.db");
      FileOutputStream fos = new FileOutputStream(db);
      int len = -1;
      byte[] buffer = new byte[1024];
      while ((len = is.read(buffer)) != -1) {
        fos.write(buffer, 0, len);
        fos.flush();
      }
      fos.close();
      is.close();
    } catch (IOException e) {
      e.printStackTrace();
      System.exit(0);
    }
  }
  return new CityDB(context, path);
}

代码示例来源:origin: coltondrg/Android-SudoInstaller

public static boolean ExtractToAppCache(Context context, String fileAsset, String output) {
  Log.i(LOGTAG, "Starting extraction of asset file \"" + fileAsset + "\"...");
  Log.i(LOGTAG, "The file will be extracted as \"" + context.getCacheDir() + "/" + output + "\"");
  try {
    InputStream in = context.getAssets().open(fileAsset);
    OutputStream out = new FileOutputStream(new File(context.getCacheDir() + "/" + output));
    try {
      copyFile(in, out);
      in.close();
      out.flush();
      out.close();
      Log.i(LOGTAG, "Successfully extracted asset file!");
      return true;
    } catch (IOException e) {
      OutputStream outputStream = out;
      Log.e(LOGTAG, "Failed to extract asset file!");
      return false;
    }
  } catch (IOException e2) {
    Log.e(LOGTAG, "Failed to extract asset file!");
    return false;
  }
}

代码示例来源:origin: NASAWorldWind/WorldWindAndroid

in = new BufferedInputStream(context.getAssets().open(fileName));
out = new BufferedOutputStream(new FileOutputStream(file));
while ((count = in.read(buffer)) != -1) {
  out.write(buffer, 0, count);
out.flush();

代码示例来源:origin: tiann/understand-plugin-framework

/**
 * 把Assets里面得文件复制到 /data/data/files 目录下
 *
 * @param context
 * @param sourceName
 */
public static void extractAssets(Context context, String sourceName) {
  AssetManager am = context.getAssets();
  InputStream is = null;
  FileOutputStream fos = null;
  try {
    is = am.open(sourceName);
    File extractFile = context.getFileStreamPath(sourceName);
    fos = new FileOutputStream(extractFile);
    byte[] buffer = new byte[1024];
    int count = 0;
    while ((count = is.read(buffer)) > 0) {
      fos.write(buffer, 0, count);
    }
    fos.flush();
  } catch (IOException e) {
    e.printStackTrace();
  } finally {
    closeSilently(is);
    closeSilently(fos);
  }
}

代码示例来源:origin: commonsguy/cw-omnibus

in=ctxt.getAssets().open("cover.pdf");
out=new FileOutputStream(destination.getFileDescriptor());
while ((size=in.read(buf)) >= 0
  && !cancellationSignal.isCanceled()) {
 out.write(buf, 0, size);
 in.close();
 out.close();

代码示例来源:origin: janishar/PlaceHolderView

private static String loadJSONFromAsset(Context context, String jsonFileName) {
  String json = null;
  InputStream is=null;
  try {
    AssetManager manager = context.getAssets();
    Log.d(TAG,"path "+jsonFileName);
    is = manager.open(jsonFileName);
    int size = is.available();
    byte[] buffer = new byte[size];
    is.read(buffer);
    is.close();
    json = new String(buffer, "UTF-8");
  } catch (IOException ex) {
    ex.printStackTrace();
    return null;
  }
  return json;
}

代码示例来源:origin: TommyLemon/Android-ZBLibrary

private static CityDB openCityDB(Context context, String packageName) {
  String path = "/data"
      + Environment.getDataDirectory().getAbsolutePath()
      + File.separator + packageName + File.separator
      + CityDB.CITY_DB_NAME;
  File db = new File(path);
  if (!db.exists()) {
    try {
      InputStream is = context.getAssets().open("city.db");
      FileOutputStream fos = new FileOutputStream(db);
      int len = -1;
      byte[] buffer = new byte[1024];
      while ((len = is.read(buffer)) != -1) {
        fos.write(buffer, 0, len);
        fos.flush();
      }
      fos.close();
      is.close();
    } catch (IOException e) {
      e.printStackTrace();
      System.exit(0);
    }
  }
  return new CityDB(context, path);
}

代码示例来源:origin: BuildmLearn/BuildmLearn-Toolkit-Android

/**
 * @param context              Application context
 * @param assetFileName        Name of the file stored in assets
 * @param destinationDirectory Destination folder for saving the file
 * @brief Copies a file from assets folder to a folder on device memory
 */
public static void copyAssets(Context context, String assetFileName, String destinationDirectory) {
  AssetManager assetManager = context.getAssets();
  InputStream in;
  OutputStream out;
  try {
    in = assetManager.open(assetFileName);
    File f = new File(destinationDirectory);
    if (!f.isDirectory()) {
      f.mkdirs();
    }
    File outFile = new File(destinationDirectory, assetFileName);
    out = new FileOutputStream(outFile);
    copyFile(in, out);
    in.close();
    out.flush();
    out.close();
  } catch (IOException e) {
    e.printStackTrace();
  }
}

代码示例来源:origin: meefik/busybox

AssetManager assetManager = c.getAssets();
InputStream in = null;
OutputStream out = null;
  in = assetManager.open(rootAsset + path);
  String fullPath = PrefStore.getEnvDir(c) + path;
  out = new FileOutputStream(fullPath);
  byte[] buffer = new byte[1024];
  int read;
  while ((read = in.read(buffer)) != -1) {
    out.write(buffer, 0, read);
  out.flush();
} catch (IOException e) {
  e.printStackTrace();

代码示例来源:origin: stackoverflow.com

InputStream myInput = dbContext.getAssets().open(DATABASE_NAME);
String outFileName = DATABASE_PATH + DATABASE_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
while ((length = myInput.read(buffer))>0){
  myOutput.write(buffer, 0, length);
myOutput.flush();
myOutput.close();
myInput.close();

相关文章

Context类方法