我可以使用一些帮助来理解如何在android中打开文件.我的具体问题与打开图像文件有关.在我的应用程序中,用户使用他们选择的相机应用程序拍摄图像,然后我对返回的图像进行 *** 作.根据手机,Android版本和所选的相机应用程序,我在onActivityResult中返回不同的参数.有时我会得到一个URI,有时只是一个图像,有时两者都有.
启动相机的代码是:
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);startActivityForResult(i, CAMERA_IMAGE);
然后我收到结果:
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来获取文件.
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.
我的具体问题是:如何修改此文件以独立于文件系统的位置打开文件,只知道URI?我想做的事情如下:
file imagefile = new file(imageUri);
但是file不接受Uri对象.我执行托管查询将其转换为String.
我更一般的问题是为什么我首先需要进行查询?为什么我不能只使用返回的URI?
解决方法:
您必须使用contentResolver来访问以uri身份传递的内部文件
ContentResolver cr = getContentResolver();inputStream is = cr.openinputStream(imageUri);
总结 以上是内存溢出为你收集整理的从Uri打开文件,独立于android中的位置全部内容,希望文章能够帮你解决从Uri打开文件,独立于android中的位置所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)