如何从图库中检索图像的 Picasa ID/URL

发布于 2024-11-28 12:57:57 字数 424 浏览 1 评论 0原文

我有一个活动,从设备的图库中检索图像并将其上传到服务。现在,出于优化目的,我希望避免上传 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。有什么想法吗?

I have an activity that retrieves images from the device's gallery and uploads to a service. Now, for optimisation purposes, I would like to avoid uploading images that are on Picasa an just store their ID or URL for later retrieval.

So my question is, how do I retrieve that information. My intent code is pasted below and retrieves the URI of the image.

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

I tried to look for the PICASA_ID (MediaStore.Images.Media.PICASA_ID), but by using the method above, it returns null. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

堇色安年 2024-12-05 12:57:57
  • 启动 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;
}
  • Launch an ACTION_GET_CONTENT intent instead of an ACTION_PICK

  • Provide a MediaStore.EXTRA_OUTPUT extra with an URI to a temporary file.


Add this to your calling activity:

File yourFile;

Now use this 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

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;
}
又爬满兰若 2024-12-05 12:57:57
@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...
            }

我也用过同样的方法,它很有效。希望它也能帮助你..

@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...
            }

I have used the same approach and it woks.Hope It could help u too..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文