Example usage for android.content ContextWrapper ContextWrapper

List of usage examples for android.content ContextWrapper ContextWrapper

Introduction

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

Prototype

public ContextWrapper(Context base) 

Source Link

Usage

From source file:com.breadwallet.presenter.fragments.MainFragmentQR.java

public static File getQRImageFile(Activity app) {
    ContextWrapper cw = new ContextWrapper(app);
    // path to /data/data/yourapp/app_data/imageDir
    File directory = cw.getDir("qrcodes", Context.MODE_PRIVATE);
    return new File(directory, "qrImage.jpeg");

}

From source file:com.breadwallet.presenter.fragments.MainFragmentQR.java

private void saveBitmapToFile() {

    ContextWrapper cw = new ContextWrapper(getActivity());
    // path to /data/data/yourapp/app_data/imageDir
    File directory = cw.getDir("qrcodes", Context.MODE_PRIVATE);
    // Create imageDir
    File mypath = new File(directory, "qrImage.jpeg");

    FileOutputStream fos = null;//from  w w  w.j  a  v  a  2  s  .c o  m
    try {
        fos = new FileOutputStream(mypath);
        // Use the compress method on the BitMap object to write image to the OutputStream
        if (bitmap != null)
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

From source file:com.csounds.examples.tests.MultiTouchXYActivity.java

public void storeRAWFilesToInternalStorage() throws IOException {

    InputStream in = getResources().openRawResource(R.raw.record01);
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
        File file = new File(this.getFilesDir(), "csoundtracks");
        ContextWrapper cw = new ContextWrapper(this);
        File directory = cw.getExternalFilesDir(null);
        File wavfile = new File(directory, "record01.wav");
        if (!file.mkdirs()) {
            Log.e("logtag", "Directory not created");
        }/*from   w ww  .  j  a  v a2 s.  c o  m*/
        System.out.println(wavfile.getAbsolutePath());
        System.out.println("wavfile" + wavfile + " directory " + directory);
        if (!wavfile.exists()) {
            Log.e("logtag", "wave file not created");
            try {

                wavfile.createNewFile();

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (!wavfile.exists()) {
            Log.e("logtag", "wave2 file not created");

        }

        FileOutputStream out = new FileOutputStream(wavfile);

        byte[] buff = new byte[1024];
        int read = 0;

        try {
            while ((read = in.read(buff)) > 0) {
                out.write(buff, 0, read);
            }
        } finally {
            in.close();
            out.close();
        }

    }
}

From source file:com.csounds.examples.tests.MultiTouchXYActivity.java

private void writeToFile(String data) {
    try {/*from  ww w.  jav  a  2 s. c o  m*/
        if (!txtfile.exists()) {
            Log.d("txtfile", "txtfile doesn't exist");
            ContextWrapper cw = new ContextWrapper(this);
            File directory = cw.getExternalFilesDir(null);
            txtfile = new File(directory, "temp.txt");
        }
        System.out.println("zz" + txtfile);
        FileOutputStream outStream = new FileOutputStream(txtfile, true);
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outStream);

        String output = String.format(data);
        //        BufferedWriter oFile = new BufferedWriter(new OutputStreamWriter(
        //            new FileOutputStream("test.txt"), "UTF-16"));
        outputStreamWriter.append("\r\n");
        outputStreamWriter.append(output);
        outputStreamWriter.close();

    } catch (IOException e) {
        Log.e("Exception", "File write failed: " + e.toString());
    }
}

From source file:com.policestrategies.calm_stop.citizen.ProfileDisplayActivity.java

private void loadProfileImage() {

    ContextWrapper cw = new ContextWrapper(getApplicationContext());
    File directory = cw.getDir("ProfilePic", Context.MODE_PRIVATE);

    String path = directory.getAbsolutePath();
    File f = new File(path, "profilepic.JPG");

    if (convertUriToBitmap(Uri.fromFile(f)) != null) {
        mProfileImageView.setImageBitmap(convertUriToBitmap(Uri.fromFile(f)));
        mProfileImage.setImageBitmap(convertUriToBitmap(Uri.fromFile(f)));
    } else//  w w  w. j  ava 2s .  c om
        Toast.makeText(this, "whyyyyy", Toast.LENGTH_SHORT).show();
}

From source file:co.dilaver.quoter.activities.ShareActivity.java

private void save() {

    previewLayout.setDrawingCacheEnabled(true);
    previewLayout.buildDrawingCache();//from ww w  . jav  a 2  s .c  o m
    Bitmap bmp = Bitmap.createBitmap(previewLayout.getDrawingCache());
    previewLayout.setDrawingCacheEnabled(false);

    ContextWrapper cw = new ContextWrapper(this);
    File directory = cw.getExternalFilesDir(null);
    if (!directory.exists()) {
        directory.mkdir();
    }
    File image = new File(directory, "image.jpg");

    FileOutputStream fos;
    try {
        fos = new FileOutputStream(image);
        bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
        fos.close();
    } catch (Exception e) {
        Log.e(TAG, e.getMessage(), e);
    }

    Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + image.getAbsolutePath()));
    shareIntent.setType("image/jpeg");
    startActivity(Intent.createChooser(shareIntent, getString(R.string.str_ShareWith)));
}

From source file:com.ritul.truckshare.RegisterActivity.DriverLicenseActivity.java

private String saveToInternalSorage(Bitmap bitmapImage) {
    ContextWrapper cw = new ContextWrapper(DriverLicenseActivity.this);
    // path to /data/data/yourapp/app_data/imageDir
    File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
    // Create imageDir
    File mypath = new File(directory, "tmpimage.jpg");

    FileOutputStream fos = null;/*from   w w  w . j  a v  a2  s  .  c  o m*/
    try {
        fos = new FileOutputStream(mypath);
        // Use the compress method on the BitMap object to write image to the OutputStream
        bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
        fos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return mypath.getPath();
}

From source file:com.parse.f8.view.SignInActivity.java

private String saveToInternalSorage(Bitmap bitmapImage) {
    ContextWrapper cw = new ContextWrapper(ctx);
    // path to /data/data/yourapp/app_data/imageDir
    File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
    // Create imageDir
    File mypath = new File(directory, "profile.jpg");

    FileOutputStream fos = null;/*from  w w w .j  a v a 2  s  .  co  m*/
    try {

        fos = new FileOutputStream(mypath);

        // Use the compress method on the BitMap object to write image to the OutputStream
        bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
        fos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return directory.getAbsolutePath();
}

From source file:net.robotmedia.acv.ui.widget.OcrLayout.java

/** Initialization routine called by the constructor. */
private void init(Context context) {
    // Exit if in the Eclipse graphical editor
    if (this.isInEditMode()) {
        return;//from w  w  w  .  jav a2 s .co m
    }

    this.context = context;

    this.readOcrSettings(context);

    this.determineScreenDimensions(context);

    File externalDir = this.context.getExternalFilesDir(null);
    this.tesseractDbDir = new File(externalDir.toString() + Constants.TESSERACT_ROOT_DIR);
    this.tesseractDbFile = new File(externalDir.toString() + Constants.TESSDATA_DIR,
            Constants.TESSERACT_DB_FILENAME);
    this.edictDbFile = new File(externalDir, Constants.EDICT_DB_FILENAME);
    this.namesDbFile = new File(externalDir, Constants.NAMES_DB_FILENAME);
    this.kanjiDbFile = new File(externalDir, Constants.KANJI_DB_FILENAME);
    this.kanjiDefFormatFile = new File(externalDir, Constants.KANJI_DEF_FORMAT_FILENAME);
    this.deinflectionDbFile = new File(externalDir, Constants.DEINFLECTION_DB_FILENAME);
    this.substitutionsDbFile = new File(externalDir, Constants.SUBSTITUTIONS_DB_FILENAME);
    this.freqDbFile = new File(externalDir, Constants.FREQ_DB_FILENAME);

    this.wordSetKnown = new WordSet(new File(externalDir, Constants.KNOWN_WORDS_FILENAME));
    this.wordSetTodo = new WordSet(new File(externalDir, Constants.TODO_WORDS_FILENAME));

    // Add OCR view
    this.ocrView = new OcrView(context);
    this.addView(this.ocrView);

    // Add dictionary view
    this.dicView = new WebView(context);
    this.dicView.setBackgroundColor(this.ocrSettingsDictBackgroundColor); // Make the background translucent.
    this.dicView.loadDataWithBaseURL(null, "<html><body></body></html>", "text/html", "utf-8", null);
    this.dicView.setVisibility(GONE);
    this.addView(this.dicView);

    // Set the path to eplkup
    ContextWrapper cw = new ContextWrapper(this.context);

    // Android 5.0 requires executables to be compiled with PIE (Position Independent Executable) support for security
    // reasons. However, PIE is not supported before Android 4.1, which is why we have 2 executables, one with
    // PIE and one without PIE.
    if (android.os.Build.VERSION.SDK_INT >= 20) {
        DicEpwing.setEplkupExe(cw.getApplicationInfo().dataDir + "/" + Constants.EPLKUP_FILENAME);
    } else {
        DicEpwing.setEplkupExe(cw.getApplicationInfo().dataDir + "/" + Constants.EPLKUP_NON_PIE_FILENAME);
    }
}

From source file:layout.FragmentBoardItemList.java

private void getThumbnail(final BoardItem bi) {
    bi.downloadingThumb = true;//from   w ww .j  a  va2 s .co m
    ConnectivityManager cm = (ConnectivityManager) getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = cm.getActiveNetworkInfo();
    boolean usingWifi = (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_WIFI);

    ContextWrapper cw = new ContextWrapper(getActivity().getApplicationContext());
    File directory = cw.getDir("thumbs", Context.MODE_PRIVATE);
    if (!directory.exists()) {
        directory.mkdir();
    }
    final File mypath;
    if (bi.youtubeLink) {
        mypath = new File(directory, currentBoard.getBoardDir() + "_" + bi.youtubeID);
    } else {
        mypath = new File(directory, currentBoard.getBoardDir() + "_" + bi.getThumb());
    }

    if (mypath.exists()) {
        try {
            Bitmap b = BitmapFactory.decodeStream(new FileInputStream(mypath));
            bi.setThumbBitmap(Bitmap.createScaledBitmap(b, 128, 128, false));
            listViewAdapter.notifyDataSetChanged();
            Log.i("getThumb", bi.getThumb() + " from cache");
            return;
        } catch (Exception e) {
            e.printStackTrace();
            displayError(e.getMessage());
        }
    }
    if (settings.getBoolean("setting_downloadOnlyWithWifi", false) == true && !usingWifi) {
        Log.i("getThumb", "Not using wifi");
        return;
    }
    String imgURL = "http://bienvenidoainternet.org/" + bi.getParentBoard().getBoardDir() + "/thumb/"
            + bi.getThumb();
    if (bi.getThumb().startsWith("http")) {
        imgURL = bi.getThumb();
    }
    Ion.with(getContext()).load(imgURL).setLogging("getThumbnail", Log.INFO).asBitmap()
            .setCallback(new FutureCallback<Bitmap>() {
                @Override
                public void onCompleted(Exception e, Bitmap result) {
                    if (e != null) {
                        displayError(e.getMessage());
                        e.printStackTrace();
                    } else {
                        bi.setThumbBitmap(Bitmap.createScaledBitmap(result, 128, 128, false));
                        listViewAdapter.notifyDataSetChanged();
                        FileOutputStream out;
                        try {
                            out = new FileOutputStream(mypath);
                            result.compress(Bitmap.CompressFormat.PNG, 100, out);
                            if (out != null) {
                                out.close();
                            }
                            Log.v("getThumb", bi.getThumb() + " saved.");
                        } catch (Exception e1) {
                            e1.printStackTrace();
                        }
                    }
                }
            });
}