簡體   English   中英

在Android設備(Android One設備或允許用戶將圖像保存到雲中)中獲取某些圖像的錯誤文件路徑

[英]Getting Incorrect File Path For Some Images In Android ( Android One device or which allow to user save images into cloud)

我正在嘗試從SD卡中獲取圖像並將其顯示到imageview中。 但是我的問題是,有時我會為某些圖像獲取正確的路徑,但是對於某些圖像,我會獲取不正確的路徑。

這是我的日志:

正確的道路

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

錯誤的路徑

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'

這是我的職責

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();
    }
}

更多代碼

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;

“ android-one”電話或“允許用戶將圖像保存到雲的設備”才會發生此問題。 在這種情況下,當我們單擊相機圖標以從圖庫中選擇圖像時。 因為某些圖像可能會上傳到Goolge_Cloud中並在圖庫中顯示。 很難獲得圖像路徑。 因為我是從sdcard或內部存儲器獲取圖像路徑,然后在我們的服務器中上傳。 所以我正在像這樣獲取谷歌雲的圖像URL

"https://lh5.googleusercontent.com/dOx-Uo-m3bgjHgSQ8YpxUl8DHuqjoSdpYeK8OGAW8ac=s0-d" 

當您單擊它時,它將顯示雲中的圖像。

所以我固定為

//如果我們將獲取雲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 

這是我的代碼

if (selectedImageUri.getLastPathSegment().contains("https://lh5.googleusercontent.com"))
                    doDownloadTaskAndGetSaveImage(selectedImageUri.getLastPathSegment());
                else // do as you are doing

以上步驟的任務

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();
                    }
                }

            });

}

注意我正在使用此庫下載圖像並獲取回調。 https://code.google.com/p/android-query/wiki/ImageLoading

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM