簡體   English   中英

如何從內置圖庫中檢索picasa照片?

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

我想從內置的Android圖庫中檢索調用ACTION_PICK Intent的照片。 我對Picasa的圖片有疑問。 我已將代碼用於此鏈接 ,但它不起作用(File對象不存在)。 請問任何想法。

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

使用此代碼

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");
  • 啟動ACTION_GET_CONTENT意圖而不是ACTION _PICK
  • 使用臨時文件的URI提供MediaStore.EXTRA_OUTPUT extra。

將此添加到您的通話活動中:

提交你的文件;

現在使用此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);

確保創建了yourFile

也在您的通話活動中

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