android - 检索 Picasa 图像以从图库上传

标签 android picasa

我正在进行一项 Activity 和相关任务,允许用户从图库中选择一张图像用作他们的个人资料图片。一旦做出选择,图像就会通过其 API 上传到网络服务器。我有来自图库的正常工作图像。但是,如果所选图像来自 Picasa 网络相册,则不会返回任何内容。

我做了很多调试,将问题缩小到这个方法。

public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    //cursor is null for picasa images
    if(cursor!=null)
    {
        int column_index = cursor
        .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }
    else return null;
}

Picasa 图像返回空游标。但是,MediaStore.Images.Media.DATA 对他们来说为空。它只返回一个#id,所以我猜测该地址没有实际的位图数据。 Picasa 图像是否完全存储在设备本地?

我还从文档中注意到 MediaStore.Images.ImageColumns.PICASA_ID 存在。该值存在于选定的 picasa 图像中,但不存在于其他画廊图像中。我想我可以使用这个值来获取图像的 URL,如果它不存储在本地,但我无法在任何地方找到关于它的任何信息。

最佳答案

我遇到了完全相同的问题,
最后我找到的解决方案是启动一个 ACTION_GET_CONTENT Intent 而不是 ACTION_PICK,然后确保你提供一个额外的 MediaStore.EXTRA_OUTPUT 和一个临时文件的 uri。 这是启动 Intent 的代码:

public class YourActivity extends Activity {
    File mTempFile;
    int REQUEST_CODE_CHOOSE_PICTURE = 1;

    (...)
    public showImagePicker() { 
        mTempFile = getFileStreamPath("yourTempFile");
        mTempFile.getParentFile().mkdirs();
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
        intent.setType("image/*");
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mTempFile));
        intent.putExtra("outputFormat",Bitmap.CompressFormat.PNG.name());                       
        startActivityForResult(intent,REQUEST_CODE_CHOOSE_PICTURE);
    }

    (...)
}

您可能需要 mTempFile.createFile()

然后在onActivityResult中,就可以这样获取图片了

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    case REQUEST_CODE_CHOOSE_PICTURE:
                Uri imageUri = data.getData();
                if (imageUri == null || imageUri.toString().length() == 0) {
                    imageUri = Uri.fromFile(mTempFile);
                    file = mTempFile;
                }
                                    if (file == null) {
                                       //use your current method here, for compatibility as some other picture chooser might not handle extra_output
                                    }
}

希望对你有帮助

然后你应该在完成时删除你的临时文件(它原样在内部存储中,但你可以使用外部存储,我想这会更好)。

关于android - 检索 Picasa 图像以从图库上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5944282/

相关文章:

android - Gradle的依赖项缓存似乎已损坏或不同步

java - Android 共享首选项

google-api - 如何访问 Google 相册共享相册

php - 如何处理 PHP 请求中的长 header 声明?

python - 如何从 Picasa API 获取相册网址?

android - 为什么在 Activity 开始时调用 onResume()?

android - NullPointerException android ListView

actionscript-3 - 通过http请求actionscript 3将新照片上传到picasa

automation - 从 Picasa 导出面孔

java - TextView 对齐两行