如何从图库中检索图片的Picasa ID /网址

时间:2011-08-08 07:21:43

标签: android url gallery android-intent picasa

我有一项活动,可以从设备的图库中检索图片并上传到服务。现在,出于优化目的,我希望避免上传Picasa上的图片,只是存储他们的ID或URL以便以后检索。

所以我的问题是,如何检索该信息。我的意图代码粘贴在下面并检索图像的URI。

Intent galleryIntent = new Intent();
galleryIntent.setType("image/*");
galleryIntent.setAction(Intent.ACTION_PICK);
startActivityForResult(galleryIntent, GALLERY_PIC_REQUEST);

我试图寻找PICASA_ID(MediaStore.Images.Media.PICASA_ID),但是通过使用上面的方法,它返回null。有什么想法吗?

2 个答案:

答案 0 :(得分:0)

  • 发布ACTION_GET_CONTENT意图而不是ACTION_PICK

  • 向临时文件提供MediaStore.EXTRA_OUTPUT额外的URI


将此添加到 调用活动

File yourFile;

现在使用此 代码获取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;
}

答案 1 :(得分:-1)

@Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        dir =new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/MyImages");
        dir.mkdir();
        filename = ("Image_" + String.valueOf(System.currentTimeMillis()) + ".poc");
    }

protected Uri getTempFile()
    {
        File file = new File(dir,filename);
        muri = Uri.fromFile(file);
        return muri;
     } 


    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        menu.add("Pick Image");
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) 
    {
         // TODO Auto-generated method stub
         super.onOptionsItemSelected(item);
         openOptionsChooseDialog();
         return true;
    }

 private void openOptionsChooseDialog()
    {
            AlertDialog.Builder builder = new AlertDialog.Builder(AppActivity.this).setTitle("Select Image").setItems(items, new DialogInterface.OnClickListener()
            {
                public void onClick(DialogInterface dialog, int item)
                {
                    Intent intent = new Intent();
   intent.setAction(Intent.ACTION_PICK);
                        intent.setType("image/*");
                        intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
                        intent.putExtra(MediaStore.EXTRA_OUTPUT, getTempFile());
                        startActivityForResult(intent, SELECT_PICTURE);
 }

            });
            final AlertDialog alert = builder.create();
            alert.show();
  }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
        super.onActivityResult(requestCode, resultCode, data);

        switch(requestCode)
        {
case SELECT_PICTURE : if (resultCode == RESULT_OK) 
            {
                  filepath = muri.getPath();
                  Toast.makeText(this, filepath, Toast.LENGTH_SHORT).show();
                //can do bla bla bla...
            }

我使用了相同的方法并且它有效。希望它也可以帮助你..