Android 如何从内置图库中检索picasa照片?

Android 如何从内置图库中检索picasa照片?,android,android-intent,android-gallery,Android,Android Intent,Android Gallery,我想通过调用ACTION\u PICKIntent从内置Android gallery检索照片。 我对Picasa的图像有问题。 我已经使用了代码来解决这个问题,但它不起作用(File对象不存在)。 任何想法,请。如果插入此说明,代码将起作用: intent.putExtra("crop", "true"); 启动操作\u获取\u内容意图,而不是操作\u拾取 为临时文件提供一个带有URI的MediaStore.EXTRA\u输出 将此添加到您的呼叫活动中: 把你的文件归档 现在使用此代码

我想通过调用
ACTION\u PICK
Intent从内置Android gallery检索照片。 我对Picasa的图像有问题。 我已经使用了代码来解决这个问题,但它不起作用(File对象不存在)。
任何想法,请。

如果插入此说明,代码将起作用:

 intent.putExtra("crop", "true");
  • 启动
    操作\u获取\u内容
    意图,而不是
    操作
    \u拾取
  • 为临时文件提供一个带有URI的
    MediaStore.EXTRA\u输出
    
将此添加到您的呼叫活动中:

把你的文件归档

现在使用此
代码获取意图

yourFile = getFileStreamPath("yourTempFile");
yourFile.getParentFile().mkdirs();
Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT, null);
galleryIntent .setType("image/*");
galleryIntent .putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(yourFile));
galleryIntent.putExtra("outputFormat", Bitmap.CompressFormat.PNG.name());
startActivityForResult(galleryIntent, GALLERY_PIC_REQUEST);
确保已创建
您的文件

也在您的呼叫活动中

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch(requestCode){
    case GALLERY_PIC_REQUEST:
        File file = null;
        Uri imageUri = data.getData();
        if (imageUri == null || imageUri.toString().length() == 0) {
            imageUri = Uri.fromFile(mTempFile);
            file = mTempFile;
            //this is the file you need! Check it
        }
        //if the file did not work we try alternative method
        if (file == null) {
            if (requestCode == 101 && data != null) {
                Uri selectedImageUri = data.getData();
                String selectedImagePath = getPath(selectedImageUri);
                //check this string to extract picasa id
            }
        }
    break;
    }
}

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

ACTIVITYRESULT_CHOOSEPICTURE是调用startActivity(intent,requestCode)时使用的int

使用此代码

final Uri tempUri = data.getData();
                    Uri imageUri = null;
                    final InputStream imageStream;
                    try {
                        imageStream = getActivity().getContentResolver().openInputStream(tempUri);
                        Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
                        imageUri = getImageUri(getActivity(), selectedImage);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }


public Uri getImageUri(Context inContext, Bitmap inImage) {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
        String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
        return Uri.parse(path);
    }

mTempFile文件永远都是空的。感谢您的帮助,即使图片来自picasa,它也可以正常工作。记住在线程上执行此代码,因为它可能从网络下载内容。
final Uri tempUri = data.getData();
                    Uri imageUri = null;
                    final InputStream imageStream;
                    try {
                        imageStream = getActivity().getContentResolver().openInputStream(tempUri);
                        Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
                        imageUri = getImageUri(getActivity(), selectedImage);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }


public Uri getImageUri(Context inContext, Bitmap inImage) {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
        String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
        return Uri.parse(path);
    }