Java Code Examples for android.app.Activity#getExternalFilesDir()

The following examples show how to use android.app.Activity#getExternalFilesDir() . These examples are extracted from open source projects. 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
public static void captureScreenshot(Activity activity, String subdir, String name)
{
    subdir = subdir.trim();
    if (!subdir.isEmpty() && !subdir.startsWith("/"))
    {
        subdir = "/" + subdir;
    }

    // saves to..
    //     SD card\Android\data\com.forrestguice.suntimeswidget\files\Pictures\test-screenshots\subdir
    File d = activity.getExternalFilesDir(DIRECTORY_PICTURES);
    if (d != null)
    {
        String dirPath = d.getAbsolutePath() + "/" + SCREENSHOT_DIR + subdir;

        File dir = new File(dirPath);
        boolean dirCreated = dir.mkdirs();

        String path = dirPath + "/" + name + ".png";
        File file = new File(path);
        if (file.exists())
        {
            boolean fileDeleted = file.delete();
            if (!fileDeleted) {
                Log.w("captureScreenshot", "Failed to delete file! " + path);
            }
        }

        try {
            Falcon.takeScreenshot(activity, file);
            MediaScannerConnection.scanFile(activity, new String[]{file.getAbsolutePath()}, null, null);

        } catch (Exception e1) {
            Log.e("captureScreenshot", "Failed to write file! " + e1);
        }
    } else {
        Log.e("captureScreenshot", "Failed to write file! getExternalFilesDir() returns null..");
    }
}
 
Example 2
/**
 * 创建图片文件,名称不重复。
 * @remark 图片注解,比如crop就是裁剪图、compress就是压缩图
 */
private static File createImageFile(Activity activity, @Nullable String remark) {
    File imageFile = null;
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.CHINA).format(new Date());
    String imageFileName = "AVATAR" + timeStamp + remark;
    //应用内部目录
    File pictureDir = activity.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    if(pictureDir != null) {
        File storageDir = new File(pictureDir.getAbsolutePath() + "/pictures");
        try {
            imageFile = File.createTempFile(imageFileName, ".jpg", storageDir);
        } catch (IOException e) {
            Logger.e(e.getMessage());
        }
    }
    return imageFile;
}
 
Example 3
Source Project: LockDemo   File: Ocr.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Intent方式截图处理
 */
public static void handleImgShot(Activity activity, Uri uri) {
    File file = new File(activity.getExternalFilesDir(Environment.DIRECTORY_PICTURES), System.currentTimeMillis() + ".jpg");
    String cropImagePath = file.getAbsolutePath();
    Intent intent = new Intent("com.android.camera.action.CROP");
    intent.setDataAndType(uri, "image/*");
    intent.putExtra("crop", "true");
    intent.putExtra("aspectX", 1);
    intent.putExtra("aspectY", 1);
    intent.putExtra("outputX", 500);
    intent.putExtra("outputY", 500);
    intent.putExtra("scale", true);
    intent.putExtra("scaleUpIfNeeded", true);
    intent.putExtra("return-data", false);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
    intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
    intent.putExtra("noFaceDetection", true);
    activity.startActivityForResult(intent, IMAGE_CROP_CODE);

}
 
Example 4
Source Project: OsmGo   File: CameraUtils.java    License: MIT License 6 votes vote down vote up
public static File createImageFile(Activity activity, boolean saveToGallery) throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir;
    if(saveToGallery) {
        Log.d(getLogTag(), "Trying to save image to public external directory");
        storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    }  else {
        storageDir = activity.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    }

    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );

    return image;
}
 
Example 5
Source Project: AideHelper   File: FileUtil.java    License: MIT License 5 votes vote down vote up
/**
 * 获取应用外置储存路径
 */
public static String getExternalFilesDirPath(Activity activity) {
  File file = activity.getExternalFilesDir("");
  if (file != null) {
    return file.getPath();
  } else {
    String path = Environment.getExternalStorageDirectory().getAbsolutePath();
    File appPath = new File(path + "Android/data/me.tvcfish.xposed.aidehelper/files");
    if (!appPath.exists()) {
      appPath.mkdirs();
    }
    return appPath.getPath();
  }
}
 
Example 6
private void setFile(Activity activity) throws IOException {
    mImageName = TimeUtils.getNowString() + ".jpg";
    mImageFolder = activity.getExternalFilesDir(mNoteId);
    mImageFile = new File(mImageFolder, mImageName);

    checkImageFolder();
}
 
Example 7
Source Project: tindroid   File: MessagesFragment.java    License: Apache License 2.0 5 votes vote down vote up
private File createImageFile(Activity activity) throws IOException {
    // Create an image file name
    String imageFileName = "IMG_" +
            new SimpleDateFormat("yyMMdd_HHmmss", Locale.getDefault()).format(new Date()) + "_";
    File storageDir = activity.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File imageFile = File.createTempFile(
            imageFileName,  // prefix
            ".jpg",  // suffix
            storageDir      // directory
    );

    // Make sure directories exist.
    File path = imageFile.getParentFile();
    if (path != null) {
        path.mkdirs();
    }

    return imageFile;
}
 
Example 8
Source Project: TakePhoto   File: TImageFiles.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 获取临时文件
 *
 * @param context
 * @param photoUri
 * @return
 */
public static File getTempFile(Activity context, Uri photoUri) throws TException {
    String minType = getMimeType(context, photoUri);
    if (!checkMimeType(context, minType)) {
        throw new TException(TExceptionType.TYPE_NOT_IMAGE);
    }
    File filesDir = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    if (!filesDir.exists()) {
        filesDir.mkdirs();
    }
    File photoFile = new File(filesDir, UUID.randomUUID().toString() + "." + minType);
    return photoFile;
}