【问题标题】:"java.io.FileNotFoundException (No such file or directory)"“java.io.FileNotFoundException(没有这样的文件或目录)”
【发布时间】:2019-04-07 01:36:39
【问题描述】:

我的项目中有一个类可以上传图像并保存在我的数据库中,如果成功,则将其保存在本地。这可行,我可以将图像保存在我在我的 android 手机上创建的自定义文件夹中。

但是,在另一个课程中,我正在尝试以文本形式下载编码图像并使用我上面使用的方法将其保存在本地,但它不再起作用了。好像无法创建文件夹。

我不明白它是如何在我的其他班级而不是在这个班级工作的

我尝试检查我的清单文件,看看我是否包含了所需的权限以及它们是否在正确的位置

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="buenaventura.dorothy.tbmgmtapp">

    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/lungs"
        android:label="@string/app_name"
        android:networkSecurityConfig="@xml/network_security_config"
        tools:replace="android:icon">
...

[编辑] 好的,我尝试删除文件路径中的空格以检查空格是否真的是导致问题的原因,但它仍然没有工作

File folder = new File(Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_DCIM) + File.separator + "TBSystem" + File.separator + patient_id + File.separator + "lab_result_" + sqlDateToString(result_date) + ".jpg");

另外,为什么它在我的其他带有空格的类上起作用,而不是在这个类上起作用? :(

File folder = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DCIM) + File.separator + "TB System" + File.separator + patient_id);

if (!folder.exists()) {
    folder.mkdirs();
}

folder = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DCIM) + File.separator + "TB System" + File.separator + patient_id + File.separator + "lab_result_" + result_date + ".jpg");

byte[] decodedBytes = Base64.decode(encoded_image, 0);
Bitmap imageBitmap = BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.length);

try (
    FileOutputStream out = new FileOutputStream(folder)) {
    imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out); 
} catch (IOException e) {
    e.printStackTrace();
}

W/System.err: java.io.FileNotFoundException: /storage/emulated/0/DCIM/TB System/pat1/lab_result_2019-04-06.jpg (No such file or directory)
W/System.err:     at java.io.FileOutputStream.open0(Native Method)
        at java.io.FileOutputStream.open(FileOutputStream.java:308)
        at java.io.FileOutputStream.<init>(FileOutputStream.java:238)
        at java.io.FileOutputStream.<init>(FileOutputStream.java:180)
        at buenaventura.dorothy.tbmgmtapp.db_volley.VolleyDownloadRequest$1.onResponse(VolleyDownloadRequest.java:569)
        at buenaventura.dorothy.tbmgmtapp.db_volley.VolleyDownloadRequest$1.onResponse(VolleyDownloadRequest.java:103)
        at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:82)
        at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:29)
        at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:102)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:6981)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)

patient_id 和 result_date 只是字符串。 encoded_image 是一个 lontext

我需要能够将我下载的编码文本转换为位图并将其保存在我希望在 DCIM 中找到的自定义文件夹“TB System”中。应在 TB System 中创建另一个文件夹(即 pat1、pat2、pat3...),并将图像保存在其中。

【问题讨论】:

    标签: android file android-intent android-permissions


    【解决方案1】:

    我看到您使用的路径在“TB”和“System”之间包含一个空格,这可能会导致问题。您的其他类是否也包含带空格的文件路径?如果没有,这可能是问题所在。

    查看这个other question,它有助于处理文件路径中的空格。答案从未得到批准,所以我不确定是否真的解决了这个问题,但他们提到了这个 sn-p 代码:

    Uri uri = Uri.parse(paths);
    File file = new File(uri.getPath());
    

    Uri.parse 修复了所有空格/反斜杠/非法字符问题 在路径中并产生一个“好”的 uri。

    【讨论】:

    • 是的,我的其他类也包含带空格的文件路径。不过,它确实有效。
    • 好吧,你不能像那样删除空间 - 如果文件夹被称为 TB System,那么如果你尝试访问 TBSystem,你的代码给出 fileNotFound 异常是正常的。因为您提到另一个类确实有效,所以也许发布该类以及该类的代码输出,以便我们可以比较这两个类?
    • 如果该目录不存在,我实际上是在尝试创建该目录。工作的类基本上使用确切的方法。最初,该目录不存在,当用户上传图片时,该应用会在我的安卓手机中创建一个本地目录
    • @dorothyanne 我已经更新了解决方案,让我知道它是否适合你。
    【解决方案2】:

    您是否检查了读写权限。

    READ_EXTERNAL_STORAGE 和 WRITE_EXTERNAL_STORAGE 必须以编程方式签入

    使用这个

        private boolean checkPermission(){
          if(ActivityCompat.checkSelfPermission(this, 
            Manifest.permission.READ_EXTERNAL_STORAGE) != 
            PackageManager.PERMISSION_GRANTED &&
                ActivityCompat.checkSelfPermission(this, 
            Manifest.permission.WRITE_EXTERNAL_STORAGE) != 
            PackageManager.PERMISSION_GRANTED){
            return false;
        }
        return true;
    }
    

    如果是真的做你的工作

    如果为false则调用该方法

    private static final int MY_PERMISSION_REQUEST_CODE = 1001;
    
    ActivityCompat.requestPermissions(this, new String[]{
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE
            }, MY_PERMISSION_REQUEST_CODE);
    

    并添加此代码

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode){
            case MY_PERMISSION_REQUEST_CODE:
                if(grantResults.length > 0 && grantResults[0] == 
                 PackageManager.PERMISSION_GRANTED)
                    //Do your work
                break;
        }
    }
    

    【讨论】:

    • 我也可以在凌空请求中这样做吗?
    • 这将是您的项目有一个启动页面。此方法在启动页面调用。仅在第一次播放时调用。
    【解决方案3】:

    您的代码有问题,我已经测试了文件夹中空格的组合以及它正在工作的确切文件名。基于您的问题的工作代码 sn-p 如下,

    图片下载成功快照

    代码布局视图

    build.gradle

    apply plugin: 'com.android.application'
    
    android {
        compileSdkVersion 28
        defaultConfig {
            applicationId "com.zackdawood.albummanager"
            minSdkVersion 15
            targetSdkVersion 28
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            }
        }
    }
    
    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation 'com.android.support:appcompat-v7:28.0.0'
        implementation 'com.android.support.constraint:constraint-layout:1.1.3'
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'com.android.support.test:runner:1.0.2'
        androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    }
    

    字符串.xml

    <resources>
        <string name="app_name">AlbumManager</string>
        <string name="write_button">Write Image</string>
        <string name="read_button">Read Image</string>
        <string name="request_access">Request Access</string>
    </resources>
    

    external_files.xml

    <?xml version="1.0" encoding="utf-8"?>
    <paths xmlns:android="http://schemas.android.com/apk/res/android">
        <external-path name="external_files" path="."/>
    </paths>
    

    network_security_config.xml

    <?xml version="1.0" encoding="utf-8"?>
    <network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">maven.apache.org</domain>
        <domain includeSubdomains="true">upload.wikimedia.org</domain>
    </domain-config>
    </network-security-config>
    

    GenericFileProvider.java

    package com.zackdawood.albummanager;
    
    import android.support.v4.content.FileProvider;
    
    public class GenericFileProvider extends FileProvider {
    }
    

    FileDownloader.java

    package com.zackdawood.albummanager;
    
    import android.util.Log;
    
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;
    
    public class FileDownloader {
    
        private static final String TAG = "FileDownloader";
    
        private static final int MEGABYTE = 1024 * 1024;
    
        public static void downloadFile(String fileUrl, File directory) {
            try {
                Log.v(TAG, "downloadFile() invoked ");
                Log.v(TAG, "downloadFile() fileUrl " + fileUrl);
                Log.v(TAG, "downloadFile() directory " + directory);
    
                URL url = new URL(fileUrl);
                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                urlConnection.connect();
    
                InputStream inputStream = urlConnection.getInputStream();
                FileOutputStream fileOutputStream = new FileOutputStream(directory);
                int totalSize = urlConnection.getContentLength();
    
                byte[] buffer = new byte[MEGABYTE];
                int bufferLength = 0;
                while ((bufferLength = inputStream.read(buffer)) > 0) {
                    fileOutputStream.write(buffer, 0, bufferLength);
                }
                fileOutputStream.close();
                Log.v(TAG, "downloadFile() completed ");
    
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                Log.e(TAG, "downloadFile() error" + e.getMessage());
                Log.e(TAG, "downloadFile() error" + e.getStackTrace());
            } catch (MalformedURLException e) {
                e.printStackTrace();
                Log.e(TAG, "downloadFile() error" + e.getMessage());
                Log.e(TAG, "downloadFile() error" + e.getStackTrace());
            } catch (IOException e) {
                e.printStackTrace();
                Log.e(TAG, "downloadFile() error" + e.getMessage());
                Log.e(TAG, "downloadFile() error" + e.getStackTrace());
            }
        }
    }
    

    MainActivity.java

    package com.zackdawood.albummanager;
    
    import android.content.ActivityNotFoundException;
    import android.content.Context;
    import android.content.Intent;
    import android.content.pm.PackageManager;
    import android.net.Uri;
    import android.os.AsyncTask;
    import android.os.Build;
    import android.os.Bundle;
    import android.os.Environment;
    import android.support.v4.app.ActivityCompat;
    import android.support.v4.content.FileProvider;
    import android.support.v7.app.AppCompatActivity;
    import android.util.Log;
    import android.view.View;
    import android.widget.Toast;
    
    import java.io.File;
    import java.io.IOException;
    
    public class MainActivity extends AppCompatActivity {
    
        private static final String TAG = "MainActivity";
        private static final String[] PERMISSIONS = {android.Manifest.permission.READ_EXTERNAL_STORAGE,
                android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
                android.Manifest.permission.INTERNET};
    
        private static boolean hasPermissions(Context context, String... permissions) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
                for (String permission : permissions) {
                    if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
                        return false;
                    }
                }
            }
            return true;
        }
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            ActivityCompat.requestPermissions(MainActivity.this, PERMISSIONS, 112);
            Log.v(TAG, "onCreate() Method invoked ");
        }
    
        public void write(View view) {
            if (!hasPermissions(MainActivity.this, PERMISSIONS)) {
    
                Log.v(TAG, "download() Method DON'T HAVE PERMISSIONS ");
    
                Toast t = Toast.makeText(getApplicationContext(), "You don't have write access !", Toast.LENGTH_LONG);
                t.show();
    
            } else {
    
                new DownloadFile().execute("https://upload.wikimedia.org/wikipedia/commons/thumb/c/c2/Adler.jpg/547px-Adler.jpg", "lab_result_2019-04-06.jpg");
    
    
            }
        }
    
        public void read(View view) {
            if (!hasPermissions(MainActivity.this, PERMISSIONS)) {
    
                Log.v(TAG, "download() Method DON'T HAVE PERMISSIONS ");
    
                Toast t = Toast.makeText(getApplicationContext(), "You don't have read access !", Toast.LENGTH_LONG);
                t.show();
    
            } else {
    
                File folder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
    
                File directory = new File(folder + "/TB System");
                File imgFile = new File(directory, "lab_result_2019-04-06.jpg");
    
                Log.v(TAG, "view() Method imgfile " + imgFile.getAbsolutePath());
    
                Uri path = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".fileprovider", imgFile);
    
    
                Log.v(TAG, "view() Method path " + path);
    
                Intent imageIntent = new Intent(Intent.ACTION_VIEW);
                imageIntent.setDataAndType(path, "image/*");
                imageIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                imageIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    
                try {
                    startActivity(imageIntent);
                } catch (ActivityNotFoundException e) {
                    Toast.makeText(MainActivity.this, "No Application available to view Image", Toast.LENGTH_SHORT).show();
                }
    
            }
        }
    
        public void request(View view) {
            ActivityCompat.requestPermissions(MainActivity.this, PERMISSIONS, 112);
    
        }
    
        private class DownloadFile extends AsyncTask<String, Void, Void> {
    
            @Override
            protected Void doInBackground(String... strings) {
                Log.v(TAG, "doInBackground() Method invoked ");
    
                String fileUrl = strings[0];   // -> http://maven.apache.org/maven-1.x/maven.pdf
                String fileName = strings[1];  // -> maven.pdf
                File folder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
    
                File directory = new File(folder + "/TB System");
                if (!directory.exists()) {
                    directory.mkdir();
                    Log.v(TAG, "doInBackground() Method Directory Created ");
    
                }
    
                File imgFile = new File(directory, fileName);
                Log.v(TAG, "doInBackground() imgFile invoked " + imgFile.getAbsolutePath());
                Log.v(TAG, "doInBackground() imgFile invoked " + imgFile.getAbsoluteFile());
    
                try {
                    imgFile.createNewFile();
                    Log.v(TAG, "doInBackground() file created" + imgFile);
    
                } catch (IOException e) {
                    e.printStackTrace();
                    Log.e(TAG, "doInBackground() error" + e.getMessage());
                    Log.e(TAG, "doInBackground() error" + e.getStackTrace());
    
    
                }
                FileDownloader.downloadFile(fileUrl, imgFile);
                Log.v(TAG, "doInBackground() file download completed");
    
                return null;
            }
        }
    
    }
    

    activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
    
        <LinearLayout
            android:layout_width="395dp"
            android:layout_height="715dp"
            android:orientation="vertical"
            tools:layout_editor_absoluteX="8dp"
            tools:layout_editor_absoluteY="8dp">
    
            <Button
                android:id="@+id/button2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/write_button"
                android:onClick="write"/>
    
            <Button
                android:id="@+id/button3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/read_button"
                android:onClick="read"/>
    
            <Button
                android:id="@+id/button4"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/request_access"
                android:onClick="request"/>
        </LinearLayout>
    </android.support.constraint.ConstraintLayout>
    

    AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.zackdawood.albummanager">
    
        <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
        <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
    
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme"
            android:networkSecurityConfig="@xml/network_security_config">
            <provider
                android:name=".GenericFileProvider"
                android:authorities="${applicationId}.fileprovider"
                android:exported="false"
                android:grantUriPermissions="true">
                <meta-data
                    android:name="android.support.FILE_PROVIDER_PATHS"
                    android:resource="@xml/external_files"/>
            </provider>
            <activity android:name=".MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    
    </manifest>
    

    应用启动器屏幕

    读取图像输出

    【讨论】:

      【解决方案4】:

      在访问或写入文件之前,请创建其父文件和文件本身,以确保安全。 在这种情况下...就在 try catch 块之前,或者至少在 FileOutputStream 行之前,通过if (!folder.exists()) folder.createNewFile();创建文件

      您需要存储权限 (READ-WRITE EXTERNAL STORAGE) 才能在应用的数据目录之外创建文件

      这可能会有所帮助

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-12-25
        • 1970-01-01
        • 1970-01-01
        • 2019-10-24
        • 1970-01-01
        • 2018-06-04
        • 1970-01-01
        相关资源
        最近更新 更多