代码之家  ›  专栏  ›  技术社区  ›  Naruto

如何在android中读取特定路径上的文件?

  •  0
  • Naruto  · 技术社区  · 9 年前

    我正在开发一个应用程序,在这个应用程序中,我打开文件浏览器,选择我选择的任何文件并检索其内容。打开的默认路径是/storage/sdcard0。我可以读取直接位于该路径中的任何文件的内容。但是,对于包含在/storage/sdcard0/中的任何文件夹中的任何文件。无法访问。程序给出了一个文件未找到错误。此外,我无法理解这些文件的路径,例如,如果文件位于路径中:

    /存储/sdcard0/DCIM/100ANDRO/DSC_0001.jpg

    下面是代码段:

    @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 。非常感谢您的帮助。

    2 回复  |  直到 9 年前
        1
  •  0
  •   Gavin Zhuang    9 年前

    您可以尝试以下操作:

        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
    
        2
  •  0
  •   JTeam    9 年前

    你可以试试这个,

    1. make sure you have added permission in you manifest file
    2. Settings -> Apps -> Your App -> Permissions -> Storage = true/enabled