本文介绍了从乌里打开文件,独立于Android中的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以利用一些帮助了解如何在Android开放的文件。我的具体问题与打开一个图像文件的事情。在我的应用程序,用户需要的图像与自己选择的相机应用,然后我返回的图像上进行操作。根据不同的手机,Android版本,并选择了相机应用上,我得到的onActivityResult返回不同的参数。有时候,我得到一个URI,有时只是一个图像,有时两者。

I could use some help understanding how to open files in android. My specific problem has to do with opening an image file. In my application the user takes an image with a camera app of their choosing and then I operate on the image that is returned. Depending on the phone, version of android, and the camera app chosen, I get different parameters returned in onActivityResult. Sometimes I get a URI, sometimes just an image, and sometimes both.

在code发动相机:

Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, CAMERA_IMAGE);

然后我收到结果为:

I then receive the result as:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == CAMERA_IMAGE && resultCode == Activity.RESULT_OK) {
        Log.d(TAG,"In onActivityResult");
        Bitmap imageBmp = null;
        Uri imageUri = data.getData();

        if (data.getExtras() != null) {
        imageBmp = (Bitmap)data.getExtras().get("data");
        Log.d(TAG,"Got Bitmap");
        }
        ...
    }
}

当我得到一个URI,但不是一个图像我的问题就出现了。如果imageBmp为null,则我需要从URI加载图像。我测试过这一点,一些设备/应用组合。有时URI是在内部存储和其他时间在SD卡上。如果该文件是SD卡,然后我用managedQuery获取文件上。

My problem arises when I get a URI but not an image. If imageBmp is null then I need to load the image from the URI. I've tested this out several device/app combinations. Sometimes the URI is on the internal storage and other times on the SD card. If the file is on the SD card then I've used managedQuery to get the file.

String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(imageUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
imageFileName = cursor.getString(column_index);
File imageFile = new File(imageFileName);
...

如果它在内部存储,然后我得到一个FileNotFoundException异常。

If it's on the internal storage, then I get a FileNotFoundException.

我的具体问题是:如何修改这个开独立地方是在文件系统上,只需要知道URI的文件?我愿做这样的事情:

My specific question is: How do I modify this to open a file independent of where it is on the file system, only knowing the URI? I would like to do something like:

File imageFile = new File(imageUri);

但文件不接受Uri对象。我做的托管查询将其转换为一个字符串。

but File doesn't accept a Uri object. I do the managed query to convert it to a String.

我的更普遍的问题是,为什么我需要做查询摆在首位?为什么我不能只使用返回的URI?

My more general question is why do I need to do the query in the first place? Why can't I just use the URI that is returned?

推荐答案

您将不得不使用一个ContentResolver的访问为URI通过内部文件

You will have to use a contentResolver to access internal files passed as uri

ContentResolver cr = getContentResolver();
InputStream is = cr.openInputStream(imageUri);

这篇关于从乌里打开文件,独立于Android中的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 21:33