Java Code Examples for android.content.Context#getDir()

The following examples show how to use android.content.Context#getDir() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Utils.java    From OpenCvFaceDetect with Apache License 2.0 7 votes vote down vote up
public static String exportResource(Context context, int resourceId, String dirname) {
    String fullname = context.getResources().getString(resourceId);
    String resName = fullname.substring(fullname.lastIndexOf("/") + 1);
    try {
        InputStream is = context.getResources().openRawResource(resourceId);
        File resDir = context.getDir(dirname, Context.MODE_PRIVATE);
        File resFile = new File(resDir, resName);

        FileOutputStream os = new FileOutputStream(resFile);

        byte[] buffer = new byte[4096];
        int bytesRead;
        while ((bytesRead = is.read(buffer)) != -1) {
            os.write(buffer, 0, bytesRead);
        }
        is.close();
        os.close();

        return resFile.getAbsolutePath();
    } catch (IOException e) {
        e.printStackTrace();
        throw new CvException("Failed to export resource " + resName
                + ". Exception thrown: " + e);
    }
}
 
Example 2
Source File: Utils.java    From real_time_circle_detection_android with MIT License 6 votes vote down vote up
public static String exportResource(Context context, int resourceId, String dirname) {
    String fullname = context.getResources().getString(resourceId);
    String resName = fullname.substring(fullname.lastIndexOf("/") + 1);
    try {
        InputStream is = context.getResources().openRawResource(resourceId);
        File resDir = context.getDir(dirname, Context.MODE_PRIVATE);
        File resFile = new File(resDir, resName);

        FileOutputStream os = new FileOutputStream(resFile);

        byte[] buffer = new byte[4096];
        int bytesRead;
        while ((bytesRead = is.read(buffer)) != -1) {
            os.write(buffer, 0, bytesRead);
        }
        is.close();
        os.close();

        return resFile.getAbsolutePath();
    } catch (IOException e) {
        e.printStackTrace();
        throw new CvException("Failed to export resource " + resName
                + ". Exception thrown: " + e);
    }
}
 
Example 3
Source File: Utils.java    From ResistorScanner with MIT License 6 votes vote down vote up
public static String exportResource(Context context, int resourceId, String dirname) {
    String fullname = context.getResources().getString(resourceId);
    String resName = fullname.substring(fullname.lastIndexOf("/") + 1);
    try {
        InputStream is = context.getResources().openRawResource(resourceId);
        File resDir = context.getDir(dirname, Context.MODE_PRIVATE);
        File resFile = new File(resDir, resName);

        FileOutputStream os = new FileOutputStream(resFile);

        byte[] buffer = new byte[4096];
        int bytesRead;
        while ((bytesRead = is.read(buffer)) != -1) {
            os.write(buffer, 0, bytesRead);
        }
        is.close();
        os.close();

        return resFile.getAbsolutePath();
    } catch (IOException e) {
        e.printStackTrace();
        throw new CvException("Failed to export resource " + resName
                + ". Exception thrown: " + e);
    }
}
 
Example 4
Source File: Utils.java    From FTCVision with MIT License 6 votes vote down vote up
public static String exportResource(Context context, int resourceId, String dirname) {
    String fullname = context.getResources().getString(resourceId);
    String resName = fullname.substring(fullname.lastIndexOf("/") + 1);
    try {
        InputStream is = context.getResources().openRawResource(resourceId);
        File resDir = context.getDir(dirname, Context.MODE_PRIVATE);
        File resFile = new File(resDir, resName);

        FileOutputStream os = new FileOutputStream(resFile);

        byte[] buffer = new byte[4096];
        int bytesRead;
        while ((bytesRead = is.read(buffer)) != -1) {
            os.write(buffer, 0, bytesRead);
        }
        is.close();
        os.close();

        return resFile.getAbsolutePath();
    } catch (IOException e) {
        e.printStackTrace();
        throw new CvException("Failed to export resource " + resName
                + ". Exception thrown: " + e);
    }
}
 
Example 5
Source File: Utils.java    From SoftwarePilot with MIT License 6 votes vote down vote up
public static String exportResource(Context context, int resourceId, String dirname) {
    String fullname = context.getResources().getString(resourceId);
    String resName = fullname.substring(fullname.lastIndexOf("/") + 1);
    try {
        InputStream is = context.getResources().openRawResource(resourceId);
        File resDir = context.getDir(dirname, Context.MODE_PRIVATE);
        File resFile = new File(resDir, resName);

        FileOutputStream os = new FileOutputStream(resFile);

        byte[] buffer = new byte[4096];
        int bytesRead;
        while ((bytesRead = is.read(buffer)) != -1) {
            os.write(buffer, 0, bytesRead);
        }
        is.close();
        os.close();

        return resFile.getAbsolutePath();
    } catch (IOException e) {
        e.printStackTrace();
        throw new CvException("Failed to export resource " + resName
                + ". Exception thrown: " + e);
    }
}
 
Example 6
Source File: Utils.java    From MOAAP with MIT License 6 votes vote down vote up
public static String exportResource(Context context, int resourceId, String dirname) {
    String fullname = context.getResources().getString(resourceId);
    String resName = fullname.substring(fullname.lastIndexOf("/") + 1);
    try {
        InputStream is = context.getResources().openRawResource(resourceId);
        File resDir = context.getDir(dirname, Context.MODE_PRIVATE);
        File resFile = new File(resDir, resName);

        FileOutputStream os = new FileOutputStream(resFile);

        byte[] buffer = new byte[4096];
        int bytesRead;
        while ((bytesRead = is.read(buffer)) != -1) {
            os.write(buffer, 0, bytesRead);
        }
        is.close();
        os.close();

        return resFile.getAbsolutePath();
    } catch (IOException e) {
        e.printStackTrace();
        throw new CvException("Failed to export resource " + resName
                + ". Exception thrown: " + e);
    }
}
 
Example 7
Source File: HotFix.java    From letv with Apache License 2.0 6 votes vote down vote up
public static void patch(Context context, String dexFileName, InputStream dexInputStream) {
    File dexPath = new File(context.getDir(Constant.JAR_IN_FOLDER_NAME, 0), dexFileName);
    if (prepareDex(dexPath, dexInputStream)) {
        String patchDexFile = dexPath.getAbsolutePath();
        if (new File(patchDexFile).exists()) {
            try {
                if (hasLexClassLoader()) {
                    injectInAliyunOs(context, patchDexFile);
                } else if (hasDexClassLoader()) {
                    injectAboveEqualApiLevel14(context, patchDexFile);
                } else {
                    injectBelowApiLevel14(context, patchDexFile);
                }
                Log.d(TAG, "dex patch is load the success");
            } catch (Exception e) {
                Log.e(TAG, e.getMessage());
            }
        }
    }
}
 
Example 8
Source File: IjkMediaPlayer.java    From JZVideoDemo with MIT License 6 votes vote down vote up
public static void loadLibrariesOnce(IjkLibLoader libLoader, Context context) {
        synchronized (IjkMediaPlayer.class) {
            if (!mIsLibLoaded) {
                if (libLoader == null)
                    libLoader = sLocalLibLoader;

//                libLoader.loadLibrary("ijkffmpeg");
                File dir = context.getDir("libs", Context.MODE_PRIVATE);
                final File soFile = new File(dir, "ijkffmpeg.so");
                System.load(soFile.getAbsolutePath());
                libLoader.loadLibrary("ijksdl");
                libLoader.loadLibrary("ijkplayer");
                mIsLibLoaded = true;
            }
        }
    }
 
Example 9
Source File: ApkInstaller.java    From GPT with Apache License 2.0 5 votes vote down vote up
/**
 * 返回 greedyporter 的根目录 /data/data/hostapp/app_greedyporter
 *
 * @param context host的context
 * @return 插件跟路径
 */
public static File getGreedyPorterRootPath(Context context) {
    File repoDir = context.getDir(PLUGIN_PATH, 0);

    if (!repoDir.exists()) {
        repoDir.mkdir();
    }
    return repoDir;
}
 
Example 10
Source File: ShieldClassLoader.java    From ApkShield with GNU General Public License v2.0 5 votes vote down vote up
public ShieldClassLoader(
		String desDexPath ,
		Context context ,
		String dexPath ,
		PathClassLoader parent )
{
	super( dexPath , parent );
	File file = new File( desDexPath );
	this.context = context;
	this.mClassLoader = parent;
	//  		
	try
	{
		List<String> libraryPathElements;
		//			Class cls = mClassLoader.getClass();
		Class cls = PathClassLoader.class;
		Field[] fields = cls.getDeclaredFields();
		Method[] methods = cls.getDeclaredMethods();
		//			Field field = mClassLoader.getClass().getDeclaredField( "pathList" );			
		//			field.setAccessible( true );
		//			Object pathList = field.get( mClassLoader );
		//			field = pathList.getClass().getDeclaredField( "nativeLibraryDirectories" );
		//			field.setAccessible( true );
		//			File[] path = (File[])field.get( pathList );
		//拿到originalclassloader  
		//			List<String> libraryPath = (List<String>)field.get( mClassLoader );
		//====================================
		File dexOutputDir = context.getDir( "dex" , 0 );
		mDexClassLoader = new ShieldDexClassLoader( file.getAbsolutePath() , dexOutputDir.getAbsolutePath() , file.getAbsolutePath() , (PathClassLoader)context.getClassLoader() );
	}
	catch( Exception e )
	{
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	// TODO Auto-generated constructor stub
}
 
Example 11
Source File: DaemonStrategyUnder21.java    From MarsDaemon with Apache License 2.0 5 votes vote down vote up
private boolean install(Context context, String destDirName, String assetsDirName, String filename) {
	File file = new File(context.getDir(destDirName, Context.MODE_PRIVATE), filename);
	if (file.exists()) {
		return true;
	}
	try {
		copyAssets(context, (TextUtils.isEmpty(assetsDirName) ? "" : (assetsDirName + File.separator)) + filename, file, "700");
		return true;
	} catch (Exception e) {
		return false;
	}
}
 
Example 12
Source File: BFileUtils.java    From AppOpsX with MIT License 5 votes vote down vote up
static File getBackupDir(Context context) {
  File externalFilesDir = context.getExternalFilesDir(DIR_NAME);
  if (externalFilesDir != null) {
    if (externalFilesDir.exists()) {
      return externalFilesDir;
    } else {
      boolean mkdirs = externalFilesDir.mkdirs();
      if (mkdirs) {
        return externalFilesDir;
      }
    }
  }
  return context.getDir(DIR_NAME, Context.MODE_PRIVATE);
}
 
Example 13
Source File: PluginInfo.java    From springreplugin with Apache License 2.0 5 votes vote down vote up
/**
 * 获取Dex(优化后)生成时所在的目录 <p>
 * 若为"纯APK"插件,则会位于app_p_od中;若为"p-n"插件,则会位于"app_plugins_v3_odex"中 <p>
 * 若支持同版本覆盖安装的话,则会位于app_p_c中; <p>
 * 注意:仅供框架内部使用
 *
 * @return 优化后Dex所在目录的File对象
 */
public File getDexParentDir() {
    // 必须使用宿主的Context对象,防止出现“目录定位到插件内”的问题
    Context context = RePluginInternal.getAppContext();
    if (isPnPlugin()) {
        return context.getDir(AppConstant.LOCAL_PLUGIN_ODEX_SUB_DIR, 0);
    } else if (getIsPendingCover()) {
        return context.getDir(AppConstant.LOCAL_PLUGIN_APK_COVER_DIR, 0);
    } else {
        return context.getDir(AppConstant.LOCAL_PLUGIN_APK_ODEX_SUB_DIR, 0);
    }
}
 
Example 14
Source File: DaemonStrategyXiaomi.java    From MarsDaemon with Apache License 2.0 5 votes vote down vote up
private boolean install(Context context, String destDirName, String assetsDirName, String filename) {
	File file = new File(context.getDir(destDirName, Context.MODE_PRIVATE), filename);
	if (file.exists()) {
		return true;
	}
	try {
		copyAssets(context, (TextUtils.isEmpty(assetsDirName) ? "" : (assetsDirName + File.separator)) + filename, file, "700");
		return true;
	} catch (Exception e) {
		return false;
	}
}
 
Example 15
Source File: Medialibrary.java    From OTTLivePlayer_vlc with MIT License 5 votes vote down vote up
public int init(Context context) {
    if (context == null)
        return ML_INIT_FAILED;
    sContext = context;
    File extFilesDir = context.getExternalFilesDir(null);
    File dbDirectory = context.getDir("db", Context.MODE_PRIVATE);
    if (extFilesDir == null || !extFilesDir.exists()
            || dbDirectory == null || !dbDirectory.canWrite())
        return ML_INIT_FAILED;
    int initCode = nativeInit(dbDirectory+ VLC_MEDIA_DB_NAME, extFilesDir+ THUMBS_FOLDER_NAME);
    mIsInitiated = initCode != ML_INIT_FAILED;
    return initCode;
}
 
Example 16
Source File: VideoConstructor.java    From CameraV with GNU General Public License v3.0 4 votes vote down vote up
public VideoConstructor(Context context) throws FileNotFoundException, IOException {
	fileBinDir = context.getDir("bin",Context.MODE_PRIVATE);		
	ffmpegCtrl = new FfmpegController(context, context.getCacheDir());
	ffmpegBinPath = ffmpegCtrl.getBinaryPath();
}
 
Example 17
Source File: DexLoaderBuilder.java    From android-classyshark with Apache License 2.0 4 votes vote down vote up
public static DexClassLoader fromBytes(Context context, final byte[] dexBytes) throws Exception {

        if (null == context) {
            throw new RuntimeException("No context provided");
        }

        String dexFileName = "internal.dex";

        final File dexInternalStoragePath = new File(context.getDir("dex", Context.MODE_PRIVATE), dexFileName);

        if (!dexInternalStoragePath.exists()) {

            prepareDex(dexBytes, dexInternalStoragePath);
        }

        final File optimizedDexOutputPath = context.getCodeCacheDir();

        DexClassLoader loader = new DexClassLoader(dexInternalStoragePath.getAbsolutePath(),
                optimizedDexOutputPath.getAbsolutePath(), null, context.getClassLoader().getParent());

        dexInternalStoragePath.delete();

        return loader;
    }
 
Example 18
Source File: WebappDirectoryManager.java    From delion with Apache License 2.0 4 votes vote down vote up
/** Returns the directory containing all of Chrome's web app data, creating it if needed. */
final File getBaseWebappDirectory(Context context) {
    return context.getDir(WEBAPP_DIRECTORY_NAME, Context.MODE_PRIVATE);
}
 
Example 19
Source File: WebappDirectoryManager.java    From 365browser with Apache License 2.0 4 votes vote down vote up
/** Returns the directory containing all of Chrome's web app data, creating it if needed. */
final File getBaseWebappDirectory(Context context) {
    return context.getDir(WEBAPP_DIRECTORY_NAME, Context.MODE_PRIVATE);
}
 
Example 20
Source File: DeprecatedPersistentBlobProvider.java    From mollyim-android with GNU General Public License v3.0 4 votes vote down vote up
private File getLegacyFile(@NonNull Context context, long id) {
  return new File(context.getDir("captures", Context.MODE_PRIVATE), id + "." + BLOB_EXTENSION);
}