Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
239 views
in Technique[技术] by (71.8m points)

android - 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. 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...