简体   繁体   English

从图库中检索要上传的Picasa图像

[英]Retrieve Picasa Image for Upload from Gallery

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. 选择完成后,图像将通过其API上载到Web服务器。 I have this working regular images from the gallery. 我有来自画廊的常规图像。 However, if the image selected is from a Picasa Web Album nothing is returned. 但是,如果所选图像来自Picasa网络相册,则不会返回任何内容。

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. Picasa图片会返回一个空游标。 MediaStore.Images.Media.DATA is not null for them, however. 但是,MediaStore.Images.Media.DATA对于它们不是 null。 It only returns an #id, so I am guessing that there is no actual bitmap data at the address. 它只返回一个#id,所以我猜测地址上没有实际的位图数据。 Are Picasa images stored locally on the device at all? Picasa图片是否本地存储在设备上?

I also noticed from the documentation that MediaStore.Images.ImageColumns.PICASA_ID exists. 我还从文档中注意到MediaStore.Images.ImageColumns.PICASA_ID存在。 This value exists for selected picasa images but not other gallery images. 此值适用于所选的picasa图像,但不适用于其他图库图像。 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. 我想我可以使用此值来获取图像的URL,如果它不是本地存储但我无法在任何地方找到任何相关信息。

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. 最后我发现的解决方案是启动ACTION_GET_CONTENT意图而不是ACTION_PICK,然后确保向临时文件提供带有uri的MediaStore.EXTRA_OUTPUT。 Here is the code to start the intent : 以下是启动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() 您可能需要mTempFile.createFile()

Then in onActivityResult, you will be able to get the image this way 然后在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
                                    }
}

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). 然后你应该在完成后删除你的临时文件(它在内部存储中,但你可以使用外部存储,我想它会更好)。

Why are you using the managedQuery() method? 为什么使用managedQuery()方法? That method is deprecated. 该方法已弃用。

If you want to convert a Uri to a Bitmap object try this code: 如果要将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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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