【问题标题】:Detect Camera photo folder检测相机照片文件夹
【发布时间】:2013-12-26 21:33:24
【问题描述】:

我的默认相机应用程序将照片保存到 /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);

【问题讨论】:

    标签: java android camera directory photo


    【解决方案1】:

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

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

    • 设备的默认相机应用是白痴编写的

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

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

    IOW,除了修改计划以不假设照片位于任何给定位置之外,您实际上无能为力。

    【讨论】:

    • 我更改了相机设置,以便将照片保存在方便的地方。我认为应用程序将其存储在其设置中,并且每个应用程序的设置都不同。我意识到他问了一个愚蠢的问题。您帮助我找到了解决问题的方法。
    • @CommonsWare:我明白你的意思。所以解决方案应该是加载所有文件夹里面都有图像,对吗?你知道怎么做吗?
    【解决方案2】:

    做这样的事情:

    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();
    

    ^^

    我的代码是外部SD卡:

    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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-22
      • 2018-07-23
      • 2019-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多