0

在我的应用程序中,我硬编码了捕获相机图像的路径。但是,这不适用于所有设备,因为不同设备的图像存储路径不同。有没有办法动态获取对所有设备都有效的路径?

这是我尝试过的:

    if (externalStorageState.equals(Environment.MEDIA_MOUNTED)

        || externalStorageState.equals(Environment.MEDIA_UNMOUNTED)

        || externalStorageState
                .equals(Environment.MEDIA_MOUNTED_READ_ONLY)) {


    File path = Environment
            .getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM

                    + "/Camera");//////////----path of captured images

    Log.d("operator", " path: " + path);

    if (path.exists()) {

        if (path.isDirectory()) {

            fo = new FileObserver(path.toString(),

                    FileObserver.CLOSE_WRITE) {

                @Override
                public void onEvent(int event, String path) {

                    SimpleDateFormat sdfDateT = new SimpleDateFormat(

                            "yy-MM-dd HH:mm:ss", Locale.US);

                    String st = sdfDateT.format(System

                            .currentTimeMillis());
                    Log.d("operator", "in event");

                    Log.d("operator", "lat: " + lat + " " + "lng: "

                            + lng + " " + "location: " + addre + " " + "time: " + st);

                    ptd.insert(st, String.valueOf(lat), String.valueOf(lng), addre);

                }
            };
4

1 回答 1

0

如果 API 级别低于 7 则使用getExternalStorageDirectory(),然后附加/Pictures以获取照片存储的路径。

对于 API 级别 > 7 使用getExternalStoragePublicDirectory (DIRECTORY_PICTURES).
你也可以看看这个链接

于 2012-04-30T09:48:39.773 回答