[英]how to get photo absolute path from removable SD card
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.
那是要说的。
the algorithm above scans photo paths from primary external storage which works correctly. 上面的算法会扫描正常工作的主要外部存储中的照片路径。 But when it comes to
但是当涉及到
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
顺便说一句,请参阅完整的外部目录列表
To get absolute photo paths from Android storage 从Android存储获取绝对照片路径
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 你可以这样尝试
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();
}
@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.