检索 Picasa 图像以从图库上传

发布于 2024-11-06 05:54:45 字数 929 浏览 3 评论 0原文

我正在开展一项活动和相关任务,允许用户从图库中选择一张图像用作其个人资料图片。一旦做出选择,图像就会通过其 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,但我在任何地方都找不到有关它的任何信息。

I am working on an activity and associated tasks that allow users to select an image to use as their profile picture from the Gallery. Once the selection is made the image is uploaded to a web server via its API. I have this working regular images from the gallery. However, if the image selected is from a Picasa Web Album nothing is returned.

I have done a lot of debugging and narrowed the problem down to this method.

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 images return a null cursor. MediaStore.Images.Media.DATA is not null for them, however. It only returns an #id, so I am guessing that there is no actual bitmap data at the address. Are Picasa images stored locally on the device at all?

I also noticed from the documentation that MediaStore.Images.ImageColumns.PICASA_ID exists. This value exists for selected picasa images but not other gallery images. I was thinking I could use this value to get a URL for the image if it is not store locally but I can not find any information about it anywhere.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

背叛残局 2024-11-13 05:54:45

我遇到了完全相同的问题,
最后我找到的解决方案是启动 ACTION_GET_CONTENT 意图而不是 ACTION_PICK,然后确保为临时文件提供带有 uri 的 MediaStore.EXTRA_OUTPUT 额外内容。
这是启动意图的代码:

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
                                    }
}

希望这有帮助

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

I have faced the exact same problem,
Finally the solution I found, was to launch an ACTION_GET_CONTENT intent instead of an ACTION_PICK, then make sure you provide a MediaStore.EXTRA_OUTPUT extra with an uri to a temporary file.
Here is the code to start the 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);
    }

    (...)
}

You might need to mTempFile.createFile()

Then in onActivityResult, you will be able to get the image this way

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
                                    }
}

Hope this helps

Then you should delete your temporary file on finish (it is in internal storage as is, but you can use external storage, I guess it would be better).

∞梦里开花 2024-11-13 05:54:45

为什么使用 managementQuery() 方法?该方法已被弃用。

如果您想将 Uri 转换为 Bitmap 对象,请尝试以下代码:

public Bitmap getBitmap(Uri uri) {

    Bitmap orgImage = null;
    try {
        orgImage = BitmapFactory.decodeStream(getApplicationContext().getContentResolver().openInputStream(uri));
    } catch (FileNotFoundException e) {
        // do something if you want
    }
    return orgImage;
}

Why are you using the managedQuery() method? That method is deprecated.

If you want to convert a Uri to a Bitmap object try this code:

public Bitmap getBitmap(Uri uri) {

    Bitmap orgImage = null;
    try {
        orgImage = BitmapFactory.decodeStream(getApplicationContext().getContentResolver().openInputStream(uri));
    } catch (FileNotFoundException e) {
        // do something if you want
    }
    return orgImage;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文