简体   繁体   English

如何从可移动SD卡获取照片绝对路径

[英]how to get photo absolute path from removable SD card

background information 背景资料

I have been writing a backup photos service, which needs to get all photo absolute paths from Android external storage (like photos stored in 'DCIM' directory and its subdirectores) and upload them to remote server. 我一直在编写备份照片服务,该服务需要从Android外部存储中获取所有照片的绝对路径(例如存储在“ DCIM”目录及其子目录中的照片)并将其上传到远程服务器。 The problem is how to get all validate photo absolute paths from Android device. 问题是如何从Android设备获取所有验证照片的绝对路径。 Since there is a vast majority of Android devices, it`s tough to ensure the get-photo-absolute-path algorithm to successfully reach all validate photos Gallery directory and traverse all photos paths inside of it. 由于存在大量的Android设备,因此很难确保get-photo-absolute-path算法能够成功到达所有验证照片库目录并遍历其中的所有照片路径。

Now my app only supports uploading photos from primary external storage (not the secondary external storage, like removable SD card). 现在,我的应用仅支持从主要外部存储设备(而不是辅助外部存储设备,如可移动SD卡)上传照片。 That`s to say. 那是要说的。

  1. if the device only has one emulated external storage (on-board flash), camera upload service can scan photo paths on it correctly. 如果设备仅具有一个模拟的外部存储(板载闪存),则相机上传服务可以正确扫描其上的照片路径。
  2. if the device only has a removable storage (like SD card), camera upload service can scan photo paths correctly as well. 如果设备仅具有可移动存储(例如SD卡),则相机上传服务也可以正确扫描照片路径。

the algorithm above scans photo paths from primary external storage which works correctly. 上面的算法会扫描正常工作的主要外部存储中的照片路径。 But when it comes to 但是当涉及到

  1. if the device both has a emulated external storage and a removable storage, camera upload service only scans photo paths on the emulated external storage (primary storage), but a majority of users save their photos to the 16G or bigger size removable SD card (secondary storage) which will be ignored by my app, that`s the problem. 如果设备同时具有模拟的外部存储和可移动存储,则相机上传服务仅扫描模拟的外部存储(主存储)上的照片路径,但是大多数用户将其照片保存到16G或更大尺寸的可移动SD卡(辅助存储空间),这将被我的应用忽略。 see the complete issue here . 看到完整的问题在这里

Code implementation 代码实施

To get absolute photo path from internal storage, I hard coded an "external directory list", 为了从内部存储获得绝对的照片路径,我对“外部目录列表”进行了硬编码,

String[] paths = {
            "/DCIM",
            "/DCIM/Camera",
            "/DCIM/100MEDIA",
            // Many Samsung phones mount the external sd card to /sdcard/external_sd
            "/external_sd/DCIM",
            "/external_sd/DCIM/Camera",
            "/external_sd/DCIM/100MEDIA"
        };

and combined the absolute path like 并结合像

String fullPath = Environment.getExternalStorageDirectory().getAbsolutePath() + path;

I know that`s not the best practice, that`s why ask for help. 我知道这不是最佳做法,因此为什么要寻求帮助。 BTW, see the complete external directory list 顺便说一句,请参阅完整的外部目录列表

Question

To get absolute photo paths from Android storage 从Android存储获取绝对照片路径

  1. check if external storage mounted, then scan photos from internal storage by default. 检查是否安装了外部存储器,然后默认情况下从内部存储器扫描照片。 This can fit a majority of getting photo path requirements, see the complete implementation here 这可以满足大多数获取照片路径的要求,请参见此处的完整实现
  2. let user choose a specific directory to upload photos from SD card (if mounted one) 让用户选择一个特定目录从SD卡上传照片(如果已安装)

So I wonder if the proposal above is right or not? 所以我想知道以上提议是否正确?
Any comments or reference will be greatly appreciated. 任何意见或参考将不胜感激。


EDIT Different manufacturers set their SD card mounted point differently, there is no regular rules for that, it almost impossible (or say, bad practice) to scan and upload photos by the app in the background automatically. 编辑不同的制造商对SD卡的安装点进行了不同的设置,没有固定的规则,几乎不可能(或者说是不良做法)通过应用程序在后台自动扫描和上传照片。 To get photos path from SD card, the practical way I think is to only scan root directories, then shows such directories in a file browser window to let user choose a specific gallery directory and persist the path locally instead of scanning by the app itself. 为了从SD卡获取照片路径,我认为实用的方法是仅扫描根目录,然后在文件浏览器窗口中显示此类目录,以使用户选择特定的图库目录并在本地保留路径,而不是通过应用程序本身进行扫描。 Because it`s error prone to scan photos directives automatically on SD card. 因为它很容易在SD卡上自动扫描照片指令。

You can try this way 你可以这样尝试

for popup 用于弹出

private void selectImage() 
{
    final CharSequence[] items = { "Camera", "Gallery","Cancel" };
    AlertDialog.Builder builder = new AlertDialog.Builder(Detail_mul.this);
    builder.setTitle("Add Photo!");
    builder.setItems(items, new DialogInterface.OnClickListener() 
    {
        @Override
        public void onClick(DialogInterface dialog, int item) 
        {
            if (items[item].equals("Camera"))
            {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                File f = new File(android.os.Environment.getExternalStorageDirectory(), "temp.jpg");
                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
                startActivityForResult(intent, REQUEST_CAMERA);
            } else if (items[item].equals("Gallery")) {
                Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                intent.setType("image/*");
                startActivityForResult(Intent.createChooser(intent, "Select File"),SELECT_FILE);
            } else if (items[item].equals("Cancel")) {
                dialog.dismiss();
            }
        }
    });
    builder.show();
}

for getting the result 为了得到结果

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Bitmap bm = null;
    if (resultCode == RESULT_OK) {
        if (requestCode == REQUEST_CAMERA) {
            File f = new File(Environment.getExternalStorageDirectory().toString());
            for (File temp : f.listFiles()) {
                if (temp.getName().equals("temp.jpg")) {
                    f = temp;
                    break;
                }
            }
            try {

                BitmapFactory.Options btmapOptions = new BitmapFactory.Options();
                bm = BitmapFactory.decodeFile(f.getAbsolutePath(),btmapOptions);
                bm = Bitmap.createScaledBitmap(bm, 300, 200, true);

                String path = android.os.Environment.getExternalStorageDirectory()+ File.separator+ "Phoenix" + File.separator + "default";

                PreferenceManager.getDefaultSharedPreferences(getBaseContext()).edit().putString("endum_image_"+count, f.toString()).commit();

                OutputStream fOut = null;
                File file = new File(path, String.valueOf(System.currentTimeMillis()) + ".jpg");
                try {
                    fOut = new FileOutputStream(file);
                    bm.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
                    fOut.flush();
                    fOut.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (requestCode == SELECT_FILE) 
        {
            Uri selectedImageUri = data.getData();

            //getRealPathFromURI(selectedImageUri);

            String tempPath = getPath(selectedImageUri, Detail_mul.this);
            PreferenceManager.getDefaultSharedPreferences(getBaseContext()).edit().putString("endum_image_"+count, tempPath).commit();

            BitmapFactory.Options btmapOptions = new BitmapFactory.Options();
            bm = BitmapFactory.decodeFile(tempPath,btmapOptions);
            bm = Bitmap.createScaledBitmap(bm, 300, 200, true);

            bm = BitmapFactory.decodeFile(tempPath, btmapOptions);

        }
    }   }

I think you are uploading images for your photo service.You can access the gallery to select a particular picture because every picture in your phone is there in your Gallery whether on SDcard or Primary memory. 我认为您正在为图片服务上传图片。您可以访问图库以选择特定图片,因为手机中的每张图片都在图库中,无论是SD卡还是主存储器。

Code for accessing gallery you can see this code.I think this would help 访问图库的代码,您可以看到代码。我认为这会有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在android棉花糖设备中如何获取可移动sd卡路径和可移动sd卡的总大小,可用大小 - in android marshmallow device how to get removable sd card path and removable sd card total size ,avilable size 在android中获取可移动SD卡路径 - get removable sd card path in android 如何从可移动 SD 卡中获取文件? - how to fetch files from removable SD card? 如何获取可移动存储/sd卡的路径? 最新的 (/storage/???? ????) 格式以及 - How to get the path of removable storage/sd card? Latest (/storage/???? ????) format as well Android:如何获得可移动SD卡的永久写入权限? - Android: How to get permanent write permission on removable sd-card? 如何在 android 中访问外部存储(可移动 SD 卡)? - How to get access to External Storage (The removable SD Card) in android? 如何在java中从android的可移动存储(SD卡)获取所有pdf文件的链接? - How to get links to all pdf files from removable storage(SD Card) of android in java? 如何从可移动/辅助SD卡中删除文件 - How to remove A file from removable/secondary SD card 如何从可移动SD卡(第二外部存储)中删除文件? - How to delete file from removable sd card (second external storage)? 如何在Android中选择可移动SD卡中的位置? - How to select location in removable SD card in Android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM