android.app.Activity.getExternalFilesDir()方法的使用及代码示例

x33g5p2x  于2022-01-15 转载在 其他  
字(10.8k)|赞(0)|评价(0)|浏览(496)

本文整理了Java中android.app.Activity.getExternalFilesDir()方法的一些代码示例,展示了Activity.getExternalFilesDir()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Activity.getExternalFilesDir()方法的具体详情如下:
包路径:android.app.Activity
类名称:Activity
方法名:getExternalFilesDir

Activity.getExternalFilesDir介绍

暂无

代码示例

代码示例来源:origin: zhihu/Matisse

@SuppressWarnings("ResultOfMethodCallIgnored")
private File createImageFile() throws IOException {
  // Create an image file name
  String timeStamp =
      new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
  String imageFileName = String.format("JPEG_%s.jpg", timeStamp);
  File storageDir;
  if (mCaptureStrategy.isPublic) {
    storageDir = Environment.getExternalStoragePublicDirectory(
        Environment.DIRECTORY_PICTURES);
    if (!storageDir.exists()) storageDir.mkdirs();
  } else {
    storageDir = mContext.get().getExternalFilesDir(Environment.DIRECTORY_PICTURES);
  }
  if (mCaptureStrategy.directory != null) {
    storageDir = new File(storageDir, mCaptureStrategy.directory);
    if (!storageDir.exists()) storageDir.mkdirs();
  }
  // Avoid joining path components manually
  File tempFile = new File(storageDir, imageFileName);
  // Handle the situation that user's external storage is not ready
  if (!Environment.MEDIA_MOUNTED.equals(EnvironmentCompat.getStorageState(tempFile))) {
    return null;
  }
  return tempFile;
}

代码示例来源:origin: crazycodeboy/TakePhoto

/**
 * 获取临时文件
 *
 * @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;
}

代码示例来源:origin: jokermonn/permissions4m

/**
 * record audio, {@link android.Manifest.permission#RECORD_AUDIO},
 * it will consume some resource!!
 *
 * @param activity
 * @return true if success
 */
private static boolean checkRecordAudio(Activity activity) throws Exception {
  AudioRecordManager recordManager = new AudioRecordManager();
  recordManager.startRecord(activity.getExternalFilesDir(Environment.DIRECTORY_RINGTONES) + "/" +
      TAG + ".3gp");
  recordManager.stopRecord();
  return recordManager.getSuccess();
}

代码示例来源:origin: JasonQS/Anti-recall

public void checkUpdate() {
  String apkPath = activity.getExternalFilesDir("") + File.separator;
  Log.e(TAG, "checkUpdateListener: apk path: " + apkPath);
  AllenVersionChecker

代码示例来源:origin: com.uphyca/android-junit4-robolectric

/**
 * @param type
 * @return
 * @see android.content.ContextWrapper#getExternalFilesDir(java.lang.String)
 */
public File getExternalFilesDir(String type) {
  return mActivity.getExternalFilesDir(type);
}

代码示例来源:origin: lessthanoptimal/BoofAndroidDemo

public static File getExternalDirectory( Activity activity ) {
    // if possible use a public directory. If that fails use a private one
//        if(Objects.equals(Environment.getExternalStorageState(), Environment.MEDIA_MOUNTED)) {
//            File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
//            if( !dir.exists() )
//                dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
//            return new File(dir,"org.boofcv.android");
//        } else {
      return activity.getExternalFilesDir(null);
//        }
  }

代码示例来源:origin: oswaldo89/EasyImagePicker-library

private File createImageFile() throws IOException {
  // Create an image file name
  String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
  String imageFileName = "JPEG_" + timeStamp + "_";
  File storageDir = mainactivity.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
  File image = File.createTempFile(
      imageFileName,  /* prefix */
      ".jpg",         /* suffix */
      storageDir      /* directory */
  );
  // Save a file: path for use with ACTION_VIEW intents
  mCurrentPhotoPath = image.getAbsolutePath();
  return image;
}

代码示例来源:origin: imLibo/FilePicker

private File createImageFile() throws IOException {
  // Create an image file name
  String timeStamp =
      new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
  String imageFileName = String.format("JPEG_%s.jpg", timeStamp);
  File storageDir;
  if (mCaptureStrategy.isPublic) {
    storageDir = Environment.getExternalStoragePublicDirectory(
        Environment.DIRECTORY_PICTURES);
  } else {
    storageDir = mContext.get().getExternalFilesDir(Environment.DIRECTORY_PICTURES);
  }
  // Avoid joining path components manually
  File tempFile = new File(storageDir, imageFileName);
  // Handle the situation that user's external storage is not ready
  if (!Environment.MEDIA_MOUNTED.equals(EnvironmentCompat.getStorageState(tempFile))) {
    return null;
  }
  return tempFile;
}

代码示例来源:origin: roomanl/AndroidDownload

private File createImageFile() throws IOException {
  // Create an image file name
  String timeStamp =
      new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
  String imageFileName = String.format("JPEG_%s.jpg", timeStamp);
  File storageDir;
  if (mCaptureStrategy.isPublic) {
    storageDir = Environment.getExternalStoragePublicDirectory(
        Environment.DIRECTORY_PICTURES);
  } else {
    storageDir = mContext.get().getExternalFilesDir(Environment.DIRECTORY_PICTURES);
  }
  // Avoid joining path components manually
  File tempFile = new File(storageDir, imageFileName);
  // Handle the situation that user's external storage is not ready
  if (!Environment.MEDIA_MOUNTED.equals(EnvironmentCompat.getStorageState(tempFile))) {
    return null;
  }
  return tempFile;
}

代码示例来源:origin: techyourchance/idocare-android

/**
 * This method invokes {@link Activity#startActivityForResult(android.content.Intent, int)}
 * with the provided request code in order to take a picture with device's camera.
 * @param requestCode a request code to be used when starting a camera activity
 * @param pictureNamePrefix if not null, this string will be prepended to picture's name
 * @return an absolute path to the file in which the picture will be stored
 */
public String takePicture(int requestCode, @Nullable String pictureNamePrefix) {
  String currDateTime =
      new SimpleDateFormat("dd-MM-yyyy_HH-mm-ss", Locale.getDefault()).format(new Date());
  String pictureName = pictureNamePrefix != null ? pictureNamePrefix + currDateTime : currDateTime;
  File outputFile = new File(mActivity
      .getExternalFilesDir(Environment.DIRECTORY_PICTURES), pictureName + ".jpg");
  Uri cameraPictureUri;
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    cameraPictureUri = FileProvider.getUriForFile(
        mActivity,
        "il.co.idocare.provider",
        outputFile);
  } else {
    cameraPictureUri = Uri.fromFile(outputFile);
  }
  Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  intent.putExtra(MediaStore.EXTRA_OUTPUT, cameraPictureUri);
  mActivity.startActivityForResult(intent, requestCode);
  return outputFile.getAbsolutePath();
}

代码示例来源:origin: Hemumu/WallpaperDemo

@Override
public void onActivityCreated(Bundle savedInstanceState) {
  super.onActivityCreated(savedInstanceState);
  mFile = new File(getActivity().getExternalFilesDir(null), "pic.jpg");
}

代码示例来源:origin: Hu12037102/ImageCompress

@SuppressWarnings("ResultOfMethodCallIgnored")
private File createImageFile() throws IOException {
  // Create an image file name
  String timeStamp =
      new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
  String imageFileName = String.format("JPEG_%s.jpg", timeStamp);
  File storageDir;
  if (mCaptureStrategy.isPublic) {
    storageDir = Environment.getExternalStoragePublicDirectory(
        Environment.DIRECTORY_PICTURES);
    if (!storageDir.exists()) storageDir.mkdirs();
  } else {
    storageDir = mContext.get().getExternalFilesDir(Environment.DIRECTORY_PICTURES);
  }
  if (mCaptureStrategy.directory != null) {
    storageDir = new File(storageDir, mCaptureStrategy.directory);
    if (!storageDir.exists()) storageDir.mkdirs();
  }
  // Avoid joining path components manually
  File tempFile = new File(storageDir, imageFileName);
  // Handle the situation that user's external storage is not ready
  if (!Environment.MEDIA_MOUNTED.equals(EnvironmentCompat.getStorageState(tempFile))) {
    return null;
  }
  return tempFile;
}

代码示例来源:origin: tzutalin/Android-Object-Detection

@Override
public void onActivityCreated(Bundle savedInstanceState) {
  super.onActivityCreated(savedInstanceState);
  mFile = new File(getActivity().getExternalFilesDir(null), "pic.jpg");
}

代码示例来源:origin: peekler/GDG

@Override
public void onActivityCreated(Bundle savedInstanceState) {
  super.onActivityCreated(savedInstanceState);
  mFile = new File(getActivity().getExternalFilesDir(null), "pic.jpg");
}

代码示例来源:origin: seemoo-lab/fitness-app

String result = "";
if(isExternalStorageReadable()){
  File path = activity.getExternalFilesDir("../../../../" + directory);
  File file = new File(path, name);
  try {

代码示例来源:origin: seemoo-lab/fitness-app

int read = 0;
if(isExternalStorageReadable()){
  File path = activity.getExternalFilesDir("/Documents/");
  File file = new File(path, name);
  int size = (int) file.length();

代码示例来源:origin: mkulesh/microMathematics

private void exportLatex(String directory, String scriptName)
{
  // Document directory and file
  final File docDir = new File(context.getExternalFilesDir(null) + "/" + directory);
  docDir.mkdir();
  final File docFile = new File(docDir, scriptName + ".tex");
  final Uri docUri = FileUtils.ensureScheme(Uri.fromFile(docFile));
  // Graphics directory
  final String GRAPHICS_DIRECTORY = "graphics";
  final File graphicsDir = new File(context.getExternalFilesDir(null) + "/" + directory + "/" + GRAPHICS_DIRECTORY);
  graphicsDir.mkdir();
  final Uri graphicsUri = FileUtils.ensureScheme(Uri.fromFile(graphicsDir));
  final AdapterFileSystem adapter = new AdapterFileSystem(context);
  adapter.setUri(graphicsUri);
  ViewUtils.Debug(this, "Exporting document " + scriptName + ", graphics uri: " + adapter.getDir());
  final Exporter.Parameters exportParameters = new Exporter.Parameters();
  exportParameters.skipDocumentHeader = true;
  exportParameters.skipImageLocale = true;
  exportParameters.imageDirectory = GRAPHICS_DIRECTORY;
  Exporter.write(formulas, docUri, FileType.LATEX, adapter, exportParameters);
}

代码示例来源:origin: doc-rj/smartcard-reader

String path = getActivity().getExternalFilesDir(null).getPath();
int idx = path.lastIndexOf("/Android");
mLogPath = (idx == -1) ? path : "<storage>" + path.substring(idx);

代码示例来源:origin: googlesamples/android-AutoBackupForApps

private ArrayList<File> createListOfFiles() {
  ArrayList<File> listOfFiles = new ArrayList<File>();
  addFilesToList(listOfFiles, getActivity().getFilesDir());
  if (Utils.isExternalStorageAvailable()) {
    addFilesToList(listOfFiles, getActivity().getExternalFilesDir(null));
  }
  addFilesToList(listOfFiles, getActivity().getNoBackupFilesDir());
  return listOfFiles;
}

代码示例来源:origin: brarcher/budget-watch

@Before
public void setUp() throws IOException
{
  // Output logs emitted during tests so they may be accessed
  ShadowLog.stream = System.out;
  activity = Robolectric.setupActivity(ImportExportActivity.class);
  db = new DBHelper(activity);
  imageDir = activity.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
  assertNotNull(imageDir);
  boolean result;
  if(imageDir.exists() == false)
  {
    result = imageDir.mkdirs();
    assertTrue(result);
  }
  missingReceipt = new File(imageDir, "missing");
  orphanReceipt = new File(imageDir, "orphan");
  result = orphanReceipt.createNewFile();
  assertTrue(result);
}

相关文章

Activity类方法