在Android中获取选定的图像文件位置 [英] Get Selected Image File Location in Android
问题描述
我在这里看过类似的帖子,但没有专门回答我的问题。基本上这是我用户点击加载图片按钮时所做的代码:
//创建一个新的Intent打开图片选择器:
Intent loadPicture = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
//启动它,运行startActivityForResult()方法:
startActivityForResult(loadPicture,SELECT_IMAGE);
从这段代码,我有一个 onActivityResult()$ c
//如果用户试图选择一个图像:
if(requestCode == SELECT_IMAGE)
{
//检查用户是否真的选择了一个图像:
if(resultCode == Activity.RESULT_OK)
{
//获得用户选择的图像的URI:
Uri selectedImage = data.getData();
//创建一个新的Intent发送到下一个Activity:
Intent i = new Intent(currentActivty.this,nextActivity.class);
// -----------------问题领域-----------------
//我想将文件名发送到Intent对象,并发送它。
//然而,selectedImage.toString()方法将返回一个
//content://字符串,而不是文件位置。如何从该URI对象获取文件
//位置?
i.putExtra(PICTURE_LOCATION,selectedImage.toString());
//开始上面的Intent概述的活动:
startActivity(i);
如上面的代码所示, uri.toString()
将返回一个 content://
字符串,而不是所选图片的文件位置。注意:另一种可能的解决方案是通过 content://
来发送文件位置。字符串并将其转换为位图
(这是在下一个活动中发生的情况)。但是,我不知道该怎么做。
我已经找到了自己的问题的答案。在做了一些更多的搜索之后,我终于在这里发现了一个帖子,在这里提出了同样的问题: android通过Uri.getPath()获取真正的路径。
不幸的是,答案有一个断开的链接。经过一番Google搜索,我发现这里的网站链接正确: http://www.androidsnippets。 org / snippets / 130 / (我已经验证过这段代码确实有效。)
然而,我决定采取不同的路线。由于我的下一个Activity正在使用 ImageView
来显示图片,我将使用Uri内容字符串来链接到下一个Activity的所有方法。
在下一个Activity中,我使用 ImageView.setImageUri()
方法。
$ b
下面是我在下一个活动中的代码,显示来自 content://
字符串的图片:
//从前面的Activity获取内容字符串:
picLocation = getIntent()。getStringExtra(PICTURE_LOCATION);
//实例化ImageView对象:
ImageView imageViewer =(ImageView)findViewById(R.id.ImageViewer);
//将Uri字符串转换为可用的Uri:
Uri temp = Uri.parse(picLocation);
imageViewer.setImageURI(temp);
我希望这个问题和答案对未来的Android开发者有帮助。 $ b
I am currently making an app which works with images. I need to implement functionality where the user picks a file stored on the SD card. Once they pick the picture (using the Android gallery), the the file-location of the image will be sent to another Activity, where other work will be done upon it.
I have seen similar posts here on SO, but none to answer my question specifically. Basically this is the code I am doing when the user clicks the "Load a Picture" button:
// Create a new Intent to open the picture selector:
Intent loadPicture = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// To start it, run the startActivityForResult() method:
startActivityForResult(loadPicture, SELECT_IMAGE);
From that code, I then have a onActivityResult()
method to listen to the call-back:
// If the user tried to select an image:
if(requestCode == SELECT_IMAGE)
{
// Check if the user actually selected an image:
if(resultCode == Activity.RESULT_OK)
{
// This gets the URI of the image the user selected:
Uri selectedImage = data.getData();
// Create a new Intent to send to the next Activity:
Intent i = new Intent(currentActivty.this, nextActivity.class);
// ----------------- Problem Area -----------------
// I would like to send the filename to the Intent object, and send it over.
// However, the selectedImage.toString() method will return a
// "content://" string instead of a file location. How do I get a file
// location from that URI object?
i.putExtra("PICTURE_LOCATION", selectedImage.toString());
// Start the activity outlined with the Intent above:
startActivity(i);
As the code above states, the uri.toString()
will return a content://
string instead of the file location of the selected picture. How do I obtain the file location?
Note: Another possible solution is to send over the content://
string and convert that into a Bitmap
(which is what happens in the next Activity). However, I don't know how to do that.
I have found the answer to my own question. After doing some more searching, I finally stumbled upon a post here on SO which asks the same question here: android get real path by Uri.getPath().
Unfortunately, the answer has a broken link. After some Google searching, I found the correct link to the site here: http://www.androidsnippets.org/snippets/130/ (I have verified that this code does indeed work.)
However, I decided to take a different route. Since my next Activity is using an ImageView
to display the picture, I am instead going to use the Uri content string for all methods that link to the next Activity.
In the next Activity, I am using the ImageView.setImageUri()
method.
Here is the code I am doing in the next Activity to display the picture from the content://
string:
// Get the content string from the previous Activity:
picLocation = getIntent().getStringExtra("PICTURE_LOCATION");
// Instantiate the ImageView object:
ImageView imageViewer = (ImageView)findViewById(R.id.ImageViewer);
// Convert the Uri string into a usable Uri:
Uri temp = Uri.parse(picLocation);
imageViewer.setImageURI(temp);
I hope that this question and answer will be helpful to future Android developers.
这篇关于在Android中获取选定的图像文件位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!