How Can I Get Thumbnails For Picasa Images Selected From The Gallery?

大城市里の小女人 提交于 2019-12-04 15:19:13

What I have found is that on my Galaxy Nexus, the images for Picassa are stored in one of subdirectories under the /sdcard/Android/data/com.google.android.apps.plus/cache directory. When the content provider is com.google.android.gallery3d.provider then the number after "item" in the URL contains the name of the image (in your example above "5634890756050069570"). This data correspondes to a file in one of the subdirectories under /sdcard/Android/data/com.google.android.apps.plus/cache with the extension ".screen". If you were to copy this image from your phone (in your case 5634890756050069570.screen) using DDMS and rename it with the extension ".jpeg" you could open it and view it on your computer.

The following onActivityResult method will check for this content provider being returned, and then will recursively search for the file in the /sdcard/Android/data/com.google.android.apps.plus/cache directory. The private member variable fileSearchPathResults is filled in by the recursive search method walkDirectoryRecursivelySearchingForFile().

private String fileSearchPathResult = null;

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };
            String filePath = null;

            // This code is required to get the image path on content providers
            // on API > 10 where the image is from a picassa web album, since Google changed
            // the content provider in versions with API > 10
            if (selectedImage.toString().contains("com.google.android.gallery3d.provider")) {
                StringBuilder contentProviderPath = new StringBuilder(selectedImage.toString());
                int beginningIndex = contentProviderPath.lastIndexOf("/");
                String fileNameWithoutExt = contentProviderPath.subSequence(beginningIndex + 1,
                        contentProviderPath.length()).toString();
                Log.i(TAG, fileNameWithoutExt);
                try {
                    File path = new File("/sdcard/Android/data/com.google.android.apps.plus/cache");
                    if (path.exists() && path.isDirectory()) {
                        fileSearchPathResult = null;
                        walkDirectoryRecursivelySearchingForFile(fileNameWithoutExt, path);
                        if (fileSearchPathResult != null) {
                            filePath = fileSearchPathResult;
                        }
                    }
                } catch (Exception e) {
                    Log.i(TAG, "Picassa gallery content provider directory not found.");
                }
            }
    }


    public void walkDirectoryRecursivelySearchingForFile(String fileName, File dir) {
        String pattern = fileName;

        File listFile[] = dir.listFiles();
        if (listFile != null) {
            for (int i = 0; i < listFile.length; i++) {
                if (listFile[i].isDirectory()) {
                    walkDirectoryRecursivelySearchingForFile(fileName, listFile[i]);
                } else {
                    if (listFile[i].getName().contains(pattern)) {
                        fileSearchPathResult = listFile[i].getPath();
                    }
                }
            }
        }
    }

With the filePath, you can create a Bitmap of the image with the following code:

        Bitmap sourceImageBitmap = BitmapFactory.decodeFile(filePath);

ACTIVITYRESULT_CHOOSEPICTURE is the int you use when calling startActivity(intent, requestCode);

public void onActivityResult(int requestCode, int resultCode, Intent data) {
  if(requestCode == ACTIVITYRESULT_CHOOSEPICTURE) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 4;
    final InputStream is = context.getContentResolver().openInputStream(intent.getData());
    final Bitmap bitmap = BitmapFactory.decodeStream(is, null, options);
    is.close();
  }
}

That code will load the whole image. You can adjust the sample size to something reasonable to get a thumbnail sized image.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!