简体   繁体   English

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

[英]How do I retrieve a picasa photo from a built-in gallery?

I want to retrieve the photos from built-in Android gallery calling ACTION_PICK Intent. 我想从内置的Android图库中检索调用ACTION_PICK Intent的照片。 I have a problem with Picasa's images. 我对Picasa的图片有疑问。 I have used the code to this link , but it don't work (the File object don't exist). 我已将代码用于此链接 ,但它不起作用(File对象不存在)。 Any idea, please. 请问任何想法。

ACTIVITYRESULT_CHOOSEPICTURE is the int you use when calling startActivity(intent, requestCode); ACTIVITYRESULT_CHOOSEPICTURE是调用startActivity时使用的int(intent,requestCode);

public void onActivityResult(int requestCode, int resultCode, Intent data) {
  if(requestCode == ACTIVITYRESULT_CHOOSEPICTURE) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    final InputStream is = context.getContentResolver().openInputStream(intent.getData());
    final Bitmap bitmap = BitmapFactory.decodeStream(is, null, options);
    is.close();
  }
}

Use this code 使用此代码

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

如果插入此指令,代码将起作用:

 intent.putExtra("crop", "true");
  • Launch an ACTION_GET_CONTENT intent instead of an ACTION _PICK 启动ACTION_GET_CONTENT意图而不是ACTION _PICK
  • Provide a MediaStore.EXTRA_OUTPUT extra with an URI to a temporary file. 使用临时文件的URI提供MediaStore.EXTRA_OUTPUT extra。

Add this to your calling activity: 将此添加到您的通话活动中:

File yourFile; 提交你的文件;

Now use this code to get Intent : 现在使用此code to get Intent

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

MAKE SURE THAT yourFile is created 确保创建了yourFile

Also in your calling activity 也在您的通话活动中

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

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

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