Example usage for android.content Context getFileStreamPath

List of usage examples for android.content Context getFileStreamPath

Introduction

In this page you can find the example usage for android.content Context getFileStreamPath.

Prototype

public abstract File getFileStreamPath(String name);

Source Link

Document

Returns the absolute path on the filesystem where a file created with #openFileOutput is stored.

Usage

From source file:com.noshufou.android.su.util.Util.java

public static String ensureSuTools(Context context) {
    File suTools = context.getFileStreamPath("sutools");
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    int sutoolsVersion = prefs.getInt("sutoolsVersion", 0);

    PackageManager pm = context.getPackageManager();
    int appVersionCode;
    try {/*from  w  w w.  j  a v  a2s  .  c  o  m*/
        PackageInfo info = pm.getPackageInfo(context.getPackageName(), 0);
        appVersionCode = info.versionCode;
    } catch (NameNotFoundException e) {
        appVersionCode = 0;
    }

    if (suTools.exists() && appVersionCode == sutoolsVersion) {
        return suTools.getAbsolutePath();
    }
    Log.d(TAG, "extracting sutools");

    try {
        copyFromAssets(context, "sutools-" + Build.CPU_ABI.split("-")[0], "sutools");
    } catch (IOException e) {
        Log.e(TAG, "Could not extract sutools");
        return null;
    }

    Process process;
    try {
        process = new ProcessBuilder().command("chmod", "700", suTools.getAbsolutePath())
                .redirectErrorStream(true).start();
        process.waitFor();
        process.destroy();
    } catch (IOException e) {
        Log.e(TAG, "Failed to set filemode of sutools");
        return null;
    } catch (InterruptedException e) {
        Log.w(TAG, "process interrupted");
    }

    prefs.edit().putInt("sutoolsVersion", appVersionCode).commit();
    return suTools.getAbsolutePath();
}

From source file:com.nickandjerry.dynamiclayoutinflator.DynamicLayoutInflator.java

public static View inflateName(Context context, String name, ViewGroup parent) {
    if (name.startsWith("<")) {
        // Assume it's XML
        return DynamicLayoutInflator.inflate(context, name, parent);
    } else {//from w w  w. ja  va  2  s .  c  o  m
        File savedFile = context.getFileStreamPath(name + ".xml");
        try {
            InputStream fileStream = new FileInputStream(savedFile);
            return DynamicLayoutInflator.inflate(context, fileStream, parent);
        } catch (FileNotFoundException e) {
        }
        try {
            InputStream assetStream = context.getAssets().open(name + ".xml");
            return DynamicLayoutInflator.inflate(context, assetStream, parent);
        } catch (IOException e) {
        }
        int id = context.getResources().getIdentifier(name, "layout", context.getPackageName());
        if (id > 0) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            return inflater.inflate(id, parent, false);
        }
    }
    return null;
}

From source file:com.daskiworks.ghwatch.backend.WatchedRepositoriesService.java

/**
 * Create service./*w w  w . j  av a 2s  . co  m*/
 * 
 * @param context this service runs in
 */
public WatchedRepositoriesService(Context context) {
    this.context = context;
    persistFile = context.getFileStreamPath(fileName);
    this.authenticationManager = AuthenticationManager.getInstance();
}

From source file:com.entertailion.android.launcher.spotlight.ProcessSpotlight.java

/**
 * Process the spotlight JSON data/*from w ww.  j  a  v  a  2  s. c o m*/
 * 
 * @param jsonFeed
 * @return
 * @throws JSONException
 */
public static List<SpotlightInfo> process(Context context, String jsonFeed) throws JSONException {
    JSONObject jsonObj = new JSONObject(jsonFeed);
    List<SpotlightInfo> result = null;
    if (null != jsonObj) {
        JSONArray models = (JSONArray) jsonObj.get("models");
        if (null != models && models.length() > 0) {
            result = new ArrayList<SpotlightInfo>();
            int position = 0;
            for (int i = 0; i < models.length(); i++) {
                JSONObject model = (JSONObject) models.get(i);
                String title = model.getString("title");
                String url = model.getString("url");
                String logo = model.getString("hdpiLogo");
                String icon = null;
                if (null == SpotlightInfo.spotlightIconMap.get(title.toLowerCase())) {
                    // icon file path
                    try {
                        icon = "spotlight_" + sanitizeName(title) + ".png";
                        File file = context.getFileStreamPath(icon);
                        if (!file.exists()) {
                            Uri uri = Uri.parse(url);
                            String alternateLogo = Utils.getWebSiteIcon(context,
                                    uri.getScheme() + "://" + uri.getHost());
                            if (alternateLogo != null && alternateLogo.trim().length() > 0) {
                                icon = alternateLogo;
                            } else {
                                // create a file-based icon from original
                                // 360x203
                                Bitmap bitmap = Utils.getBitmapFromURL(logo);
                                if (bitmap != null) {
                                    bitmap = Utils.crop(bitmap, 30, 30);
                                    Utils.saveToFile(context, bitmap, 100, 100, icon);
                                    bitmap.recycle();
                                    bitmap = null;
                                } else {
                                    icon = null;
                                }
                            }
                        }
                    } catch (Exception e) {
                        Log.e(LOG_TAG, "create spotlight icon", e);
                    }
                }
                Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                result.add(new SpotlightInfo(position++, title, browserIntent, logo, icon));
            }
        }
    }
    return result;
}

From source file:com.agateau.equiv.ui.Kernel.java

public void shareCustomProductList(Context context) {
    File file = context.getFileStreamPath(CUSTOM_PRODUCTS_CSV);
    Uri contentUri = FileProvider.getUriForFile(context, "com.agateau.equiv.fileprovider", file);
    final Resources res = context.getResources();

    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("message/rfc822");
    intent.putExtra(Intent.EXTRA_EMAIL, new String[] { Constants.CUSTOM_PRODUCTS_EMAIL });
    intent.putExtra(Intent.EXTRA_SUBJECT, res.getString(R.string.share_email_subject));
    intent.putExtra(Intent.EXTRA_STREAM, contentUri);
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

    context.startActivity(Intent.createChooser(intent, res.getString(R.string.share_via)));
}

From source file:com.daskiworks.ghwatch.backend.AuthenticationManager.java

private File getCuFile(Context context) {
    if (cuFile == null)
        cuFile = context.getFileStreamPath(cuFileName);
    return cuFile;
}

From source file:com.daskiworks.ghwatch.backend.AuthenticationManager.java

private File getCuliFile(Context context) {
    if (culiFile == null)
        culiFile = context.getFileStreamPath(culiFileName);
    return culiFile;
}

From source file:net.sf.xfd.provider.PublicProvider.java

private static @Nullable Key getSalt(Context c) {
    if (cookieSalt == null) {
        synchronized (PublicProvider.class) {
            if (cookieSalt == null) {
                try {
                    try (ObjectInputStream oos = new ObjectInputStream(c.openFileInput(COOKIE_FILE))) {
                        cookieSalt = (Key) oos.readObject();
                    } catch (ClassNotFoundException | IOException e) {
                        LogUtil.logCautiously("Unable to read key file, probably corrupted or missing", e);

                        final File corrupted = c.getFileStreamPath(COOKIE_FILE);

                        //noinspection ResultOfMethodCallIgnored
                        corrupted.delete();
                    }//  ww w  . j  ava  2s .c  o m

                    if (cookieSalt != null) {
                        return cookieSalt;
                    }

                    final KeyGenerator keygen = KeyGenerator.getInstance("HmacSHA1");
                    keygen.init(COOKIE_SIZE * Byte.SIZE);

                    cookieSalt = keygen.generateKey();

                    try (ObjectOutputStream oos = new ObjectOutputStream(
                            c.openFileOutput(COOKIE_FILE, Context.MODE_PRIVATE))) {
                        oos.writeObject(cookieSalt);
                    } catch (IOException e) {
                        LogUtil.logCautiously("Failed to save key file", e);

                        return null;
                    }
                } catch (NoSuchAlgorithmException e) {
                    throw new AssertionError("failed to initialize hash functions", e);
                }
            }
        }
    }

    return cookieSalt;
}

From source file:org.matrix.matrixandroidsdk.db.ConsoleMediasCache.java

/**
 * Save a bitmap to the local cache/*from   ww  w  .j a  v  a2s  .c o  m*/
 * it could be used for unsent media to allow them to be resent.
 * @param bitmap the bitmap to save
 * @param defaultFileName the filename is provided, if null, a filename will be generated
 * @return the media cache URL
 */
public static String saveBitmap(Bitmap bitmap, Context context, String defaultFileName) {
    String filename = "file" + System.currentTimeMillis();
    String cacheURL = null;

    try {
        if (null != defaultFileName) {
            File file = new File(defaultFileName);
            file.delete();

            filename = Uri.fromFile(file).getLastPathSegment();
        }

        FileOutputStream fos = context.openFileOutput(filename, Context.MODE_PRIVATE);

        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);

        fos.flush();
        fos.close();

        cacheURL = Uri.fromFile(context.getFileStreamPath(filename)).toString();
    } catch (Exception e) {
    }

    return cacheURL;
}

From source file:com.xiaomi.account.utils.SysHelper.java

public static File saveAsFile(Context context, InputStream imgStream, String filename) throws IOException {
    BufferedInputStream bis = new BufferedInputStream(imgStream);
    OutputStream os = new BufferedOutputStream(context.openFileOutput(filename, 0));
    byte[] buffer = new byte[1024];
    while (true) {
        try {//  w  w  w  .j  a v a  2s .  c o  m
            int count = bis.read(buffer);
            if (count == -1) {
                break;
            }
            os.write(buffer, 0, count);
        } finally {
            try {
                os.flush();
            } catch (IOException e) {
            }
            try {
                os.close();
            } catch (IOException e2) {
            }
        }
    }
    try {
        os.close();
    } catch (IOException e3) {
    }
    return context.getFileStreamPath(filename);
}