如何在Android中读取特定路径中的文件?
问题描述:
我正在开发一个应用程序,我打开文件资源管理器并选择我选择的任何文件并检索其内容。打开的默认路径是/ storage/sdcard0。我能够读取直接驻留在此路径中的任何文件的内容。但是,对于包含在/ storage/sdcard0 /中的任何文件夹中的任何文件。无法访问。该程序给出一个文件未找到错误。另外,我无法理解的路径,这些文件有,像例如,如果一个文件驻留在路径:如何在Android中读取特定路径中的文件?
/storage/sdcard0/DCIM/100ANDRO/DSC_0001.jpg
, 的logcat中显示的路径是:
内容:/media/external/images/media/84290/DSC_0001.jpg
如何将在我的Java代码中访问这个文件?
下面的代码片段:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d(TAG, "requestCode received: "+requestCode+" result code: "+resultCode);
if (requestCode == 1 && resultCode == RESULT_OK) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
String folderPath = "Anant";
String filePath = "image.jpg";
String imagePath = saveToInternalStorage(thumbnail, folderPath,
sImageName);
Log.i(TAG, "DeviceAPIS:actual path :: "
+ imagePath.trim().toString());
sendCameraData(imagePath.toString().trim(),
ptrAppContainerForCamera);
}
else if (requestCode == REQUEST_PATH){
if (resultCode == RESULT_OK) {
// Get the Uri of the selected file
Uri uri = data.getData();
Log.d(TAG, "data.getData() result line 742: " + uri);
String uriString = uri.toString();
File myFile = new File(uriString);
String path = myFile.getAbsolutePath();
String base64 ="Error";
byte[] bytesRead = base64.getBytes();
String displayName = null;
String fileName = null;
if (uriString.startsWith("content://")) {
Cursor cursor = null;
try {
cursor = mContext.getContentResolver().query(uri, null, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
displayName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
displayName = path + "/" + displayName;
Log.d(TAG, "BEFORE REPLACE: "+displayName);
int index = displayName.indexOf(':');
fileName = displayName.substring(index + 1, displayName.length());
Log.d(TAG,"displayName line 762 " + Part);
}
} finally {
cursor.close();
}
} else if (uriString.startsWith("file://")) {
Log.d(TAG, "FILE BLOCK LINE 768");
displayName = myFile.getName();
Log.d(TAG,"displayName 11" + displayName);
}
try{
File sdcard = Environment.getExternalStorageDirectory();
File readFile = new File(sdcard, fileName);
// File readFile = new File(uri);
int length = (int)readFile.length();
byte[] bytes = new byte[length];
FileInputStream in = new FileInputStream(readFile);
try{
in.read(bytes);
}finally {
in.close();
}
String contents = new String(bytes);
Log.d(TAG,"contents read :: \\n" + contents);
//convert to Base64
bytesRead = contents.getBytes("UTF-8");
base64 = Base64.encodeToString(bytesRead,Base64.DEFAULT);
}catch (Exception e){
Log.d(TAG, "THROWING EXCEPTION");
Log.e(TAG,e.getMessage(),e);
}
}
引发的异常是java.io.FileNotFoundException
。任何帮助是极大的赞赏。
答
您可以尝试如下:
String path = null;
Uri originalUri = data.getData();
try {
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = this.managedQuery(originalUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
path = cursor.getString(column_index);
} catch (Exception e) {
} finally {
cursor.close();
}
如果路径为空,你可以先得到位图,然后将它复制到你的本地路径。
ContentResolver resolver = this.getContentResolver();
Bitmap photo = MediaStore.Images.Media.getBitmap(resolver, originalUri);
.... // copy photo to your local path
答
你可以试试这个,
1. make sure you have added permission in you manifest file
2. Settings -> Apps -> Your App -> Permissions -> Storage = true/enabled
我曾面临FileNotFound的同样的问题,我能够通过上述#2解决它。
+0
嗨@JTeam,权限不是我想这里的问题。这是代码本身。 – Naruto
哇!这真的有用!万分感谢! :) :) – Naruto