Example usage for android.app Activity getDir

List of usage examples for android.app Activity getDir

Introduction

In this page you can find the example usage for android.app Activity getDir.

Prototype

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

Source Link

Usage

From source file:Main.java

public static File getDataUpdateDir(Activity act) {

    return act.getDir("update",
            Context.MODE_PRIVATE | Context.MODE_WORLD_READABLE | Context.MODE_WORLD_WRITEABLE);

}

From source file:Main.java

public static File getCacheDir(Activity act) {

    return act.getDir("cacheFile",
            Context.MODE_PRIVATE | Context.MODE_WORLD_READABLE | Context.MODE_WORLD_WRITEABLE);

}

From source file:com.normsstuff.maps4norm.Dialogs.java

public static void loadMarks(final Activity c, final Stack<LatLng> trace) {
    File[] files = c.getDir("traces", Context.MODE_PRIVATE).listFiles();

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.FROYO
            && Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
        File ext = API8Wrapper.getExternalFilesDir(c);
        // even though we checked the external storage state, ext is still sometimes null,
        // according to Play Store crash reports
        if (ext != null) {
            File[] filesExtern = ext.listFiles();
            File[] allFiles = new File[files.length + filesExtern.length];
            System.arraycopy(files, 0, allFiles, 0, files.length);
            System.arraycopy(filesExtern, 0, allFiles, files.length, filesExtern.length);
            files = allFiles;//from  w w  w . ja  v  a 2s.  c  om
        }
    }

    if (files.length == 0) {
        Toast.makeText(c, c.getString(R.string.no_files_found,
                c.getDir("traces", Context.MODE_PRIVATE).getAbsolutePath()), Toast.LENGTH_LONG).show();

    } else if (files.length == 1) {
        try {
            List<LatLng> list = Util.loadFromFile(Uri.fromFile(files[0]), (MyMapActivity) c);
            setUpDisplay((MyMapActivity) c, list);
        } catch (IOException e) {
            e.printStackTrace();
            Toast.makeText(c, c.getString(R.string.error, e.getClass().getSimpleName() + "\n" + e.getMessage()),
                    Toast.LENGTH_LONG).show();
        }
    } else {
        AlertDialog.Builder b = new AlertDialog.Builder(c);
        b.setTitle(R.string.select_file);
        final DeleteAdapter da = new DeleteAdapter(files, (MyMapActivity) c);
        b.setAdapter(da, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                try {
                    List<LatLng> list = Util.loadFromFile(Uri.fromFile(da.getFile(which)), (MyMapActivity) c);
                    dialog.dismiss();
                    setUpDisplay((MyMapActivity) c, list);
                } catch (IOException e) {
                    e.printStackTrace();
                    Toast.makeText(c,
                            c.getString(R.string.error, e.getClass().getSimpleName() + "\n" + e.getMessage()),
                            Toast.LENGTH_LONG).show();
                }
            }
        });
        b.create().show();
    }
}

From source file:com.normsstuff.maps4norm.Dialogs.java

public static void saveMarks(final Activity c, final Stack<LatLng> trace) {
    final File destination;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.FROYO
            && Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
        destination = API8Wrapper.getExternalFilesDir(c);
    } else {//from  w  w w  .j  a v  a 2  s . c o  m
        destination = c.getDir("traces", Context.MODE_PRIVATE);
    }

    AlertDialog.Builder b = new AlertDialog.Builder(c);
    b.setTitle(R.string.save);
    final View layout = c.getLayoutInflater().inflate(R.layout.dialog_enter_filename, null);
    ((TextView) layout.findViewById(R.id.location))
            .setText(c.getString(R.string.file_path, destination.getAbsolutePath() + "/"));
    b.setView(layout);
    b.setPositiveButton(R.string.save, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            try {
                String fname = ((EditText) layout.findViewById(R.id.filename)).getText().toString();
                if (fname == null || fname.length() < 1) {
                    fname = "MapsMeasure_" + System.currentTimeMillis();
                }
                final File f = new File(destination, fname + ".csv");
                Util.saveToFile(f, trace);

                Toast.makeText(c, c.getString(R.string.file_saved, f.getAbsolutePath()), Toast.LENGTH_SHORT)
                        .show();
            } catch (Exception e) {
                Toast.makeText(c,
                        c.getString(R.string.error, e.getClass().getSimpleName() + "\n" + e.getMessage()),
                        Toast.LENGTH_LONG).show();
            }
        }
    });
    b.create().show();

}

From source file:de.j4velin.mapsmeasure.Dialogs.java

/**
 * @param c     the Context//from w w w . ja v a2s  .  c  om
 * @param trace the current trace of points
 * @return the "save & share" dialog
 */
public static Dialog getSaveNShare(final Activity c, final Stack<LatLng> trace) {
    final Dialog d = new Dialog(c);
    d.requestWindowFeature(Window.FEATURE_NO_TITLE);
    d.setContentView(R.layout.dialog_save);
    d.findViewById(R.id.save).setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(final View v) {
            final File destination;
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.FROYO
                    && Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
                destination = API8Wrapper.getExternalFilesDir(c);
            } else {
                destination = c.getDir("traces", Context.MODE_PRIVATE);
            }

            d.dismiss();
            AlertDialog.Builder b = new AlertDialog.Builder(c);
            b.setTitle(R.string.save);
            final View layout = c.getLayoutInflater().inflate(R.layout.dialog_enter_filename, null);
            ((TextView) layout.findViewById(R.id.location))
                    .setText(c.getString(R.string.file_path, destination.getAbsolutePath() + "/"));
            b.setView(layout);
            b.setPositiveButton(R.string.save, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    try {
                        String fname = ((EditText) layout.findViewById(R.id.filename)).getText().toString();
                        if (fname == null || fname.length() < 1) {
                            fname = "MapsMeasure_" + System.currentTimeMillis();
                        }
                        final File f = new File(destination, fname + ".csv");
                        Util.saveToFile(f, trace);
                        d.dismiss();
                        Toast.makeText(c, c.getString(R.string.file_saved, f.getAbsolutePath()),
                                Toast.LENGTH_SHORT).show();
                    } catch (Exception e) {
                        Toast.makeText(c,
                                c.getString(R.string.error,
                                        e.getClass().getSimpleName() + "\n" + e.getMessage()),
                                Toast.LENGTH_LONG).show();
                    }
                }
            });
            b.create().show();
        }
    });
    d.findViewById(R.id.load).setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(final View v) {

            File[] files = c.getDir("traces", Context.MODE_PRIVATE).listFiles();

            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.FROYO
                    && Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
                File ext = API8Wrapper.getExternalFilesDir(c);
                // even though we checked the external storage state, ext is still sometims null, accoring to Play Store crash reports
                if (ext != null) {
                    File[] filesExtern = ext.listFiles();
                    File[] allFiles = new File[files.length + filesExtern.length];
                    System.arraycopy(files, 0, allFiles, 0, files.length);
                    System.arraycopy(filesExtern, 0, allFiles, files.length, filesExtern.length);
                    files = allFiles;
                }
            }

            if (files.length == 0) {
                Toast.makeText(c,
                        c.getString(R.string.no_files_found,
                                c.getDir("traces", Context.MODE_PRIVATE).getAbsolutePath()),
                        Toast.LENGTH_SHORT).show();
            } else if (files.length == 1) {
                try {
                    Util.loadFromFile(Uri.fromFile(files[0]), (Map) c);
                    d.dismiss();
                } catch (IOException e) {
                    e.printStackTrace();
                    Toast.makeText(c,
                            c.getString(R.string.error, e.getClass().getSimpleName() + "\n" + e.getMessage()),
                            Toast.LENGTH_LONG).show();
                }
            } else {
                d.dismiss();
                AlertDialog.Builder b = new AlertDialog.Builder(c);
                b.setTitle(R.string.select_file);
                final DeleteAdapter da = new DeleteAdapter(files, (Map) c);
                b.setAdapter(da, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        try {
                            Util.loadFromFile(Uri.fromFile(da.getFile(which)), (Map) c);
                            dialog.dismiss();
                        } catch (IOException e) {
                            e.printStackTrace();
                            Toast.makeText(c,
                                    c.getString(R.string.error,
                                            e.getClass().getSimpleName() + "\n" + e.getMessage()),
                                    Toast.LENGTH_LONG).show();
                        }
                    }
                });
                b.create().show();
            }
        }
    });
    d.findViewById(R.id.share).setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(final View v) {
            try {
                final File f = new File(c.getCacheDir(), "MapsMeasure.csv");
                Util.saveToFile(f, trace);
                Intent shareIntent = new Intent();
                shareIntent.setAction(Intent.ACTION_SEND);
                shareIntent.putExtra(Intent.EXTRA_STREAM,
                        FileProvider.getUriForFile(c, "de.j4velin.mapsmeasure.fileprovider", f));
                shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                shareIntent.setType("text/comma-separated-values");
                d.dismiss();
                c.startActivity(Intent.createChooser(shareIntent, null));
            } catch (IOException e) {
                e.printStackTrace();
                Toast.makeText(c,
                        c.getString(R.string.error, e.getClass().getSimpleName() + "\n" + e.getMessage()),
                        Toast.LENGTH_LONG).show();
            }
        }
    });
    return d;
}

From source file:com.normsstuff.maps4norm.Dialogs.java

/**
 * @param c     the Context//ww w.  j ava2 s  .  c  o m
 * @param trace the current trace of points
 * @return the "save & share" dialog
 */
public static Dialog getSaveNShare(final Activity c, final Stack<LatLng> trace) {
    final Dialog d = new Dialog(c);
    d.requestWindowFeature(Window.FEATURE_NO_TITLE);
    d.setContentView(R.layout.dialog_save);
    d.findViewById(R.id.saveMarks).setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(final View vX) {
            final File destination;
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.FROYO
                    && Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
                destination = API8Wrapper.getExternalFilesDir(c);
            } else {
                destination = c.getDir("traces", Context.MODE_PRIVATE);
            }

            d.dismiss();
            AlertDialog.Builder b = new AlertDialog.Builder(c);
            b.setTitle(R.string.save);
            final View layout = c.getLayoutInflater().inflate(R.layout.dialog_enter_filename, null);
            ((TextView) layout.findViewById(R.id.location))
                    .setText(c.getString(R.string.file_path, destination.getAbsolutePath() + "/"));
            b.setView(layout);
            b.setPositiveButton(R.string.save, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    try {
                        String fname = ((EditText) layout.findViewById(R.id.filename)).getText().toString();
                        if (fname == null || fname.length() < 1) {
                            fname = "MapsMeasure_" + System.currentTimeMillis();
                        }
                        final File f = new File(destination, fname + ".csv");
                        Util.saveToFile(f, trace);
                        d.dismiss();
                        Toast.makeText(c, c.getString(R.string.file_saved, f.getAbsolutePath()),
                                Toast.LENGTH_SHORT).show();
                    } catch (Exception e) {
                        Toast.makeText(c,
                                c.getString(R.string.error,
                                        e.getClass().getSimpleName() + "\n" + e.getMessage()),
                                Toast.LENGTH_LONG).show();
                    }
                }
            });
            b.create().show();
        }
    });
    d.findViewById(R.id.loadMarks).setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(final View v) {

            File[] files = c.getDir("traces", Context.MODE_PRIVATE).listFiles();

            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.FROYO
                    && Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
                File ext = API8Wrapper.getExternalFilesDir(c);
                // even though we checked the external storage state, ext is still sometimes null, accoring to Play Store crash reports
                if (ext != null) {
                    File[] filesExtern = ext.listFiles();
                    File[] allFiles = new File[files.length + filesExtern.length];
                    System.arraycopy(files, 0, allFiles, 0, files.length);
                    System.arraycopy(filesExtern, 0, allFiles, files.length, filesExtern.length);
                    files = allFiles;
                }
            }

            if (files.length == 0) {
                Toast.makeText(c,
                        c.getString(R.string.no_files_found,
                                c.getDir("traces", Context.MODE_PRIVATE).getAbsolutePath()),
                        Toast.LENGTH_SHORT).show();
            } else if (files.length == 1) {
                try {
                    Util.loadFromFile(Uri.fromFile(files[0]), (MyMapActivity) c);
                    d.dismiss();
                } catch (IOException e) {
                    e.printStackTrace();
                    Toast.makeText(c,
                            c.getString(R.string.error, e.getClass().getSimpleName() + "\n" + e.getMessage()),
                            Toast.LENGTH_LONG).show();
                }
            } else {
                d.dismiss();
                AlertDialog.Builder b = new AlertDialog.Builder(c);
                b.setTitle(R.string.select_file);
                final DeleteAdapter da = new DeleteAdapter(files, (MyMapActivity) c);
                b.setAdapter(da, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        try {
                            Util.loadFromFile(Uri.fromFile(da.getFile(which)), (MyMapActivity) c);
                            dialog.dismiss();
                        } catch (IOException e) {
                            e.printStackTrace();
                            Toast.makeText(c,
                                    c.getString(R.string.error,
                                            e.getClass().getSimpleName() + "\n" + e.getMessage()),
                                    Toast.LENGTH_LONG).show();
                        }
                    }
                });
                b.create().show();
            }
        }
    });
    /*        
            d.findViewById(R.id.share).setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(final View v) {
        try {
            final File f = new File(c.getCacheDir(), "MapsMeasure.csv");
            Util.saveToFile(f, trace);
            Intent shareIntent = new Intent();
            shareIntent.setAction(Intent.ACTION_SEND);
            shareIntent.putExtra(Intent.EXTRA_STREAM, FileProvider
                    .getUriForFile(c, "de.j4velin.mapsmeasure.fileprovider", f));
            shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            shareIntent.setType("text/comma-separated-values");
            d.dismiss();
            c.startActivity(Intent.createChooser(shareIntent, null));
        } catch (IOException e) {
            e.printStackTrace();
            Toast.makeText(c, c.getString(R.string.error,
                            e.getClass().getSimpleName() + "\n" + e.getMessage()),
                    Toast.LENGTH_LONG).show();
        }
    }
            });
    */
    return d;
}