简体   繁体   中英

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. 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" . 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. 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. so i was getting the image url of google cloud like as

"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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM