[英]Getting Incorrect File Path For Some Images In Android ( Android One device or which allow to user save images into cloud)
I am trying to get the images from sd-card and displaying it into imageview. 我正在尝试从SD卡中获取图像并将其显示到imageview中。 But My problem is that here some time I am getting correct path for some images but for some images, i am getting incorrect path. 但是我的问题是,有时我会为某些图像获取正确的路径,但是对于某些图像,我会获取不正确的路径。
This is my log:- 这是我的日志:
correct path 正确的道路
11-28 13:39:09.266: I/System.out(7950): filemangerstring = /external/images/media/2431
11-28 13:39:09.266: I/System.out(7950): selectedImagePath = /storage/sdcard0/Pictures/Screenshots/Screenshot_2014-11-28-13-27-26.png
11-28 13:39:09.266: I/System.out(7950): filePathImage1 = /storage/sdcard0/Pictures/Screenshots/Screenshot_2014-11-28-13-27-26.png
incorrect path 错误的路径
11-28 13:41:07.776: I/System.out(7950): filemangerstring = /0/https://lh6.googleusercontent.com/ATCu2g0Tg_myGn8oBFbjtS_D4cYNTqUUnU2jBTDiz10=s0-d
11-28 13:41:07.776: I/System.out(7950): selectedImagePath = null
11-28 13:41:07.776: I/System.out(7950): filePathImage1 = /0/https://lh6.googleusercontent.com/ATCu2g0Tg_myGn8oBFbjtS_D4cYNTqUUnU2jBTDiz10=s0-d
11-28 13:41:07.776: E/BitmapFactory(7950): Unable to decode stream: java.io.FileNotFoundException: /0/https:/lh6.googleusercontent.com/ATCu2g0Tg_myGn8oBFbjtS_D4cYNTqUUnU2jBTDiz10=s0-d: open failed: ENOENT (No such file or directory)
11-28 13:41:07.776: E/BitmapFactory(7950): Unable to decode stream: java.io.FileNotFoundException: /0/https:/lh6.googleusercontent.com/ATCu2g0Tg_myGn8oBFbjtS_D4cYNTqUUnU2jBTDiz10=s0-d: open failed: ENOENT (No such file or directory)
11-28 13:41:07.776: E/JHEAD(7950): can't open '/0/https://lh6.googleusercontent.com/ATCu2g0Tg_myGn8oBFbjtS_D4cYNTqUUnU2jBTDiz10=s0-d'
this is my function 这是我的职责
String filemanagerstring = selectedImageUri.getPath();
String selectedImagePath = getPath(selectedImageUri);
public String getPath(Uri uri) {
try {
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, proj, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} catch (Exception e) {
return uri.getPath();
}
}
more code 更多代码
case R.id.cameraWallBtn:
cameraBtn.setEnabled(false);
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
fileName = n + ".jpg";
String u = Environment.getExternalStorageDirectory().getPath()
+ "/" + fileName;
imageUri = Uri.fromFile(new File(u));
i.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
Intent chooserIntent = Intent.createChooser(intent,
"Select Picture...");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
new Intent[] { i });
startActivityForResult(chooserIntent, FIRST_IMAGE);
cameraBtn.setEnabled(true);
break;
private String filePathImage1 = null;
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case FIRST_IMAGE:
if (resultCode == Activity.RESULT_OK) {
Uri selectedImageUri;
if (data != null) {
if (data.getData() != null)
selectedImageUri = data.getData();
else
selectedImageUri = imageUri;
} else
selectedImageUri = imageUri;
try {
String filemanagerstring = selectedImageUri.getPath();
String selectedImagePath = getPath(selectedImageUri);
System.out.println("filemangerstring = "+filemanagerstring);
System.out.println("selectedImagePath = "+selectedImagePath);
if (selectedImagePath != null) {
filePathImage1 = selectedImagePath;
} else if (filemanagerstring != null) {
filePathImage1 = filemanagerstring;
} else {
Log.e("Bitmap", "Unknown path");
}
System.out.println("filePathImage1 = "+filePathImage1);
if (filePathImage1 != null) {
displayInImageViewAndRotateIfNeed(filePathImage1,
imagePreview);
} else {
imagePreview.setVisibility(View.GONE);
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"Internal error" + e.toString(), Toast.LENGTH_LONG)
.show();
Log.e(e.getClass().getName(), e.getMessage(), e);
}
}
break;
This issue will happen only "android-one" phone or "the devices which allow to user to save the images into cloud" . 仅“ android-one”电话或“允许用户将图像保存到云的设备”才会发生此问题。 In this case, when we clicked on camera icon for selecting the images from the gallery. 在这种情况下,当我们单击相机图标以从图库中选择图像时。 since some of the images may be uploaded in Goolge_Cloud and display us in gallery. 因为某些图像可能会上传到Goolge_Cloud中并在图库中显示。 there was difficulty to get the image path. 很难获得图像路径。 because i was getting the image path from sdcard or internally memory and then uploading in our server. 因为我是从sdcard或内部存储器获取图像路径,然后在我们的服务器中上传。 so i was getting the image url of google cloud like as 所以我正在像这样获取谷歌云的图像URL
"https://lh5.googleusercontent.com/dOx-Uo-m3bgjHgSQ8YpxUl8DHuqjoSdpYeK8OGAW8ac=s0-d"
when you will click on it, it will display the image which is in cloud. 当您单击它时,它将显示云中的图像。
so I fixed it like as 所以我固定为
// if we will get the cloud url then //如果我们将获取云URL,则
1)it will download it from cloud. ( a small loading bar will be displaying during download)
2)display in image view ( hide the loading bar once downloaded )
3)save temp into sdcard
4)take the path of this image
5)upload it into server
6)remove this temp image which was saved into sdcard
here is my code 这是我的代码
if (selectedImageUri.getLastPathSegment().contains("https://lh5.googleusercontent.com"))
doDownloadTaskAndGetSaveImage(selectedImageUri.getLastPathSegment());
else // do as you are doing
some above steps task 以上步骤的任务
private void doDownloadTaskAndGetSaveImage(String lastPathSegment) {
// show the progressBar.
imgeLoadingBar.setVisibility(View.VISIBLE);
//show the ImageView.
imagePreview.setVisibility(View.VISIBLE);
AQuery aq = new AQuery(imagePreview);
aq.hardwareAccelerated11();
aq.id(imagePreview).image(lastPathSegment, false, false, 0, 0,
new BitmapAjaxCallback() {
@Override
public void callback(String url, ImageView iv, Bitmap bm,
AjaxStatus status) {
System.out.println("url = " + url);
File file = new File(Environment
.getExternalStorageDirectory(), "image.jpg");
// delete exits image first.
System.out.println("file.exists() = " + file.exists());
if (file.exists())
file.delete();
// save this image into sdcard for temp.
try {
FileOutputStream fos = new FileOutputStream(file);
boolean isSaved = bm.compress(CompressFormat.JPEG,
75, fos);
if (isSaved) {
filePathImage1 = file.getPath();
System.out.println("get path = "
+ filePathImage1);
}
fos.flush();
fos.close();
displayInImageViewAndRotateIfNeed(filePathImage1,
iv);
// hide the progressBar.
imgeLoadingBar.setVisibility(View.GONE);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
NOTE I am using this library for downloading the image and getting callback. 注意我正在使用此库下载图像并获取回调。 https://code.google.com/p/android-query/wiki/ImageLoading https://code.google.com/p/android-query/wiki/ImageLoading
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.