检测相机照片文件夹

时间:2013-12-09 17:03:56

标签: java android camera directory photo

我的默认相机应用程序将照片保存到/ mnt / sdcard2 / Photo文件夹。 说,我怎么能通过代码检测这个文件夹?

我找到了这段代码,但它对我没有帮助:

TextView tv = new TextView(this);
// Returns /mnt/sdcard/Pictures
tv.setText(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath());
// Returns /mnt/sdcard/DCIM
tv.append("\n" + Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath());
setContentView(tv);

2 个答案:

答案 0 :(得分:4)

如果Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)都没有返回设备默认相机应用程序使用的目录,并且用户没有指定特定位置,那么该相机应用程序的作者就是白痴。在没有用户干预的情况下,默认相机应用应将照片存储在默认目录中(通常为DIRECTORY_DCIM默认

由于任何应用都可以选择将文件存储在任何地方,因此无法提前确定:

  • 设备的默认相机应用程序由白痴

  • 编写
  • 用户选择了一个不同的相机应用程序,可能是也可能不是由白痴写的,因为该特定的相机应用程序有理由不将图像存储在特定位置

  • 用户配置了相机应用程序(默认或其他)以将照片存储在其他位置

喵,除了修改你的计划以不假设照片驻留在任何特定位置之外,没有什么可以做的。

答案 1 :(得分:1)

做这样的事情:

imageView = (ImageView)findViewById(R.id.imageView1);

    File file = null;
    if (isExternalSDCard()) {
        file = new File(
                Environment
                        .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "photo.jpg");
    } else {
        file = new File(Environment.getDataDirectory(),
                Environment.DIRECTORY_PICTURES);
        if (!file.exists()) {
            file.mkdirs();
        }
        file = new File(file, "photo.jpg");
    }
    if (file != null && file.exists()) {
        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(file.getAbsolutePath(), options);
        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;

        Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(),
                options);
        imageView.setImageBitmap(bitmap);
    }

photo.jpg这是您图片的名称。

目录中的所有文件:

File[] filesPhotos = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES).listFiles();

^^

我的代码是ExternalSDCard:

private static boolean isExternalSDCard() {
    boolean mExternalStorageAvailable = false;
    boolean mExternalStorageWriteable = false;
    String state = Environment.getExternalStorageState();

    if (Environment.MEDIA_MOUNTED.equals(state)) {
        // We can read and write the media
        mExternalStorageAvailable = mExternalStorageWriteable = true;
    } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        // We can only read the media
        mExternalStorageAvailable = true;
        mExternalStorageWriteable = false;
    } else {
        // Something else is wrong. It may be one of many other states, but
        // all we need
        // to know is we can neither read nor write
        mExternalStorageAvailable = mExternalStorageWriteable = false;
    }
    return mExternalStorageAvailable && mExternalStorageWriteable;
}
相关问题