私はファイルエクスプローラを開き、自分が選んだファイルを選択してその内容を取得するアプリケーションを開発しています。デフォルトのパスは/ storage/sdcard0です。私はこのパスに直接存在するファイルの内容を読むことができます。ただし、/ storage/sdcard0 /内の任意のフォルダに格納されているファイルについては、アクセスできないプログラムは、ファイルが見つかりませんでしたエラーを返します。また、私はファイルがパスに存在する場合、これらのファイルは、例えばのように、持っているパスを理解することはできません。アンドロイドの特定のパスでファイルを読むには?
/storage/sdcard0/DCIM/100ANDRO/DSC_0001.jpg
、 A /media/external/images/media/84290/DSC_0001.jpg
方法:
内容:logcatをするパスを示してい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
次のとおりです。は、以下のコードです。どんな助けでも大歓迎です。あなたが以下のように試すことができ
うわー!これは本当に機能します!ありがとう、トン! :) :) – Naruto