Example usage for android.content ContextWrapper getDir

List of usage examples for android.content ContextWrapper getDir

Introduction

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

Prototype

@Override
    public File getDir(String name, int mode) 

Source Link

Usage

From source file:com.example.mediastock.util.Utilities.java

/**
 * Method to delete a media file within the typeDir directory. The directory is stored into the internal storage.
 *
 * @param type    the directory type: music, video or image
 * @param context the context//www  . j ava  2 s .  com
 * @param path    the path of the media file
 * @return true, if the file was deleted, false otherwise
 */
public static boolean deleteSpecificMediaFromInternalStorage(String type, Context context, String path) {
    ContextWrapper cw = new ContextWrapper(context.getApplicationContext());
    File dir = cw.getDir(type + "Dir", Context.MODE_PRIVATE);

    final File[] fileNames = dir.listFiles();

    boolean result = false;
    for (File file : fileNames) {
        if (file.getName().equals(path))
            result = file.delete();
    }

    return result;
}

From source file:com.example.mediastock.util.Utilities.java

/**
 * Returns the file containing the blob//from   w w  w.  j a v a  2  s.co m
 *
 * @param type    the typeDir directory, where type can be music or video
 * @param context the context
 * @param path    the path of the file
 * @return the file containing the blob
 */
public static File getFile(String type, Context context, final String path) {
    ContextWrapper cw = new ContextWrapper(context.getApplicationContext());
    File dir = cw.getDir(type + "Dir", Context.MODE_PRIVATE);

    final File[] fileNames = dir.listFiles();

    File target = null;
    for (File file : fileNames) {
        if (file.getName().equals(path))
            target = file;
    }

    return target;
}

From source file:com.example.mediastock.util.Utilities.java

/**
 * It loads an image from the storage//w  w w. ja  v a2s .  c  o m
 * @param context the context
 * @param path    the path of the image
 * @return the file containing the blob image
 */
public static File loadImageFromInternalStorage(Context context, String path) {
    ContextWrapper cw = new ContextWrapper(context.getApplicationContext());
    File imageDir = cw.getDir("imageDir", Context.MODE_PRIVATE);

    final File[] fileNames = imageDir.listFiles();

    File target = null;
    for (File file : fileNames) {
        if (file.getName().equals(path))
            target = file;
    }

    return target;
}

From source file:com.example.mediastock.util.Utilities.java

/**
 * It loads a media file from the storage.
 * @param type    the typeDir directory, where type can be music or video
 * @param context the context//from   w w w .  j a v a 2  s . c om
 * @param path    the path to the media file
 * @return a FileInputStream object that contains the media file
 */
public static FileInputStream loadMediaFromInternalStorage(String type, Context context, final String path) {
    ContextWrapper cw = new ContextWrapper(context.getApplicationContext());
    File dir = cw.getDir(type + "Dir", Context.MODE_PRIVATE);

    final File[] fileNames = dir.listFiles();

    File target = null;
    for (File file : fileNames) {
        if (file.getName().equals(path))
            target = file;
    }

    FileInputStream fis = null;
    try {

        fis = new FileInputStream(target);

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    return fis;
}

From source file:com.example.mediastock.util.Utilities.java

/**
 * It saves the image into the internal storage of the app.
 *
 * @param context the context/*from www .  ja v  a  2 s.  c o  m*/
 * @param type the directory type: video or image
 * @param bitmap the image
 * @param id the id of the image
 * @return the path of the image
 */
public static String saveImageToInternalStorage(Context context, String type, Bitmap bitmap, int id) {
    ContextWrapper cw = new ContextWrapper(context.getApplicationContext());

    File imageDir = cw.getDir("imageDir", Context.MODE_PRIVATE);

    // Create file image
    File file = new File(imageDir, type + id);

    FileOutputStream fos;
    try {

        fos = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);

        fos.flush();
        fos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return file.getName();
}

From source file:com.example.mediastock.util.Utilities.java

/**
 * Method to save a media file inside the directory typeDir. It writes the bytes of the stream into the file
 * @param type the directory type: music, video or image
 * @param context the context//from w w  w. ja  va 2  s  .  c  o  m
 * @param inputStream the stream
 * @param id the id of the media file
 * @return the path of the media file
 */
public static String saveMediaToInternalStorage(String type, Context context, InputStream inputStream, int id) {
    ContextWrapper cw = new ContextWrapper(context.getApplicationContext());

    File dir = cw.getDir(type + "Dir", Context.MODE_PRIVATE);

    // Create media file
    File file;

    if (type.equals(Utilities.MUSIC_DIR))
        file = new File(dir, type + id + ".mp3");
    else
        file = new File(dir, type + id);

    FileOutputStream fos;
    try {

        fos = new FileOutputStream(file);
        fos.write(Utilities.covertStreamToByte(inputStream));

        fos.flush();
        fos.close();
        inputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return file.getName();
}

From source file:com.sareen.squarelabs.techrumors.HTMLParser.HtmlLocalImageGetter.java

public Drawable getDrawable(String source) {
    Context context = container.getContext();
    Drawable d;/*from   w  ww . j av a 2  s .c o  m*/
    if ((imagePathList != null) && (count < imagePathList.size())) {
        ContextWrapper cw = new ContextWrapper(context.getApplicationContext());
        File imageDir = cw.getDir("imageDir", Context.MODE_PRIVATE);
        String fileName = imagePathList.get(count);
        File imagePath = new File(imageDir, fileName + ".jpg");
        Bitmap bitmap = BitmapFactory.decodeFile(imagePath.toString());
        d = new BitmapDrawable(context.getResources(), bitmap);
        float scale = getScale(d);
        d.setBounds(0, 0, (int) (d.getIntrinsicWidth() * scale), (int) (d.getIntrinsicHeight() * scale));

    }

    else {
        d = ContextCompat.getDrawable(context, R.mipmap.ic_launcher);
        d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
    }

    count++;
    return d;
    /*Context context = container.getContext();
    int id = context.getResources().getIdentifier(source, "drawable", context.getPackageName());
            
    if (id == 0)
    {
    // the drawable resource wasn't found in our package, maybe it is a stock android drawable?
    id = context.getResources().getIdentifier(source, "drawable", "android");
    }
            
    if (id == 0)
    {
    // prevent a crash if the resource still can't be found
    Log.e(HtmlTextView.TAG, "source could not be found: " + source);
    return null;
    }
    else
    {
    Drawable d = context.getResources().getDrawable(id);
    d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
    return d;
    }*/
}

From source file:org.bienvenidoainternet.app.ViewerActivity.java

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    File baiDir = new File(Environment.getExternalStorageDirectory().getPath() + "/Bai/");
    if (!baiDir.exists()) {
        baiDir.mkdir();//from   www. ja v a 2  s . co  m
    }
    if (item.getItemId() == R.id.menu_save_img) {
        BoardItemFile boardItemFile = fileList.get(imagePager.getCurrentItem());
        File to = new File(Environment.getExternalStorageDirectory().getPath() + "/Bai/" + boardItemFile.file);
        ContextWrapper cw = new ContextWrapper(getApplicationContext());
        File directory = cw.getDir("src", Context.MODE_PRIVATE);
        File fileSource = new File(directory, boardItemFile.boardDir + "_" + boardItemFile.file);
        try {
            InputStream in = new FileInputStream(fileSource);
            OutputStream out = new FileOutputStream(to);
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            in.close();
            out.close();
            Toast.makeText(getApplicationContext(), boardItemFile.file + " guardado.", Toast.LENGTH_LONG)
                    .show();
            MediaScannerConnection.scanFile(this, new String[] { to.getPath() }, new String[] { "image/jpeg" },
                    null);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    if (item.getItemId() == android.R.id.home) {
        onBackPressed();
    }
    return super.onOptionsItemSelected(item);
}

From source file:layout.FragmentImage.java

private void downloadFile() {
    downloadBar = ((ViewerActivity) getActivity()).barDownload;
    downloadBar.setVisibility(View.VISIBLE);
    ContextWrapper cw = new ContextWrapper(getContext());
    File directory = cw.getDir("src", Context.MODE_PRIVATE);
    final File filePath = new File(directory, boardItemFile.boardDir + "_" + boardItemFile.file);
    if (filePath.exists()) {
        downloadBar.setVisibility(View.GONE);
        if (boardItemFile.file.endsWith(".gif")) {
            try {
                GifDrawable gifFromFile = new GifDrawable(filePath);
                gifView.setImageDrawable(gifFromFile);
                gifView.setVisibility(View.VISIBLE);
                imageView.setVisibility(View.GONE);
            } catch (Exception e) {
                e.printStackTrace();/*  w ww. j  a  v  a  2s .c o m*/
            }
        } else {
            imageView.setImage(ImageSource.uri(filePath.toURI().getPath()));
            imageView.setVisibility(View.VISIBLE);
            gifView.setVisibility(View.GONE);
        }
    }
    Ion.with(getContext()).load(boardItemFile.fileURL).progressBar(downloadBar).asInputStream()
            .setCallback(new FutureCallback<InputStream>() {
                @Override
                public void onCompleted(Exception e, InputStream result) {
                    downloadBar.setVisibility(View.GONE);
                    if (e != null) {
                        e.printStackTrace();
                    } else {
                        FileOutputStream fout;
                        try {
                            fout = new FileOutputStream(filePath);
                            final byte data[] = new byte[1024];
                            int count;
                            while ((count = result.read(data, 0, 1024)) != -1) {
                                fout.write(data, 0, count);
                            }
                        } catch (Exception e1) {
                            e1.printStackTrace();
                        }
                        if (boardItemFile.file.endsWith(".gif")) {
                            try {
                                GifDrawable gifFromFile = new GifDrawable(filePath);
                                gifView.setImageDrawable(gifFromFile);
                                gifView.setVisibility(View.VISIBLE);
                                imageView.setVisibility(View.GONE);
                            } catch (Exception e2) {
                                e2.printStackTrace();
                            }
                        } else {
                            imageView.setImage(ImageSource.uri(filePath.toURI().getPath()));
                            gifView.setVisibility(View.GONE);
                            imageView.setVisibility(View.VISIBLE);
                        }
                    }
                }
            });
}

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  a2  s.  c om
    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();
        }
    }

}