1

当我尝试将文件存储在 sdcard 中时,我正在创建一个应用程序来录制语音

<uses-sdk android:minSdkVersion="8"

它给了我错误“没有这样的文件或目录”,但是当我从清单中注释掉“uses-sdk”部分时,它工作正常。我不确定实际问题是什么,但似乎是由于在清单中设置了 sdk-version。我也尝试增加/减少 minSdkVersion,但仍然是同样的错误。

下面是错误日志。

 11-28 15:53:54.887: W/System.err(5935): java.io.FileNotFoundException: /mnt/sdcard/Recorder/voicemail.3gp (No such file or directory)
11-28 15:53:54.897: W/System.err(5935):     at org.apache.harmony.luni.platform.OSFileSystem.open(Native Method)
11-28 15:53:54.897: W/System.err(5935):     at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:232)
11-28 15:53:54.897: W/System.err(5935):     at java.io.FileOutputStream.<init>(FileOutputStream.java:94)
11-28 15:53:54.897: W/System.err(5935):     at java.io.FileOutputStream.<init>(FileOutputStream.java:165)
11-28 15:53:54.897: W/System.err(5935):     at java.io.FileOutputStream.<init>(FileOutputStream.java:144)
11-28 15:53:54.897: W/System.err(5935):     at android.media.MediaRecorder.prepare(MediaRecorder.java:540)
11-28 15:53:54.897: W/System.err(5935):     at com.abc.SendVoiceMailFragment.startRecording(SendVoiceMailFragment.java:96)
11-28 15:53:54.897: W/System.err(5935):     at com.abc.SendVoiceMailFragment.access$0(SendVoiceMailFragment.java:84)
11-28 15:53:54.897: W/System.err(5935):     at com.abc.SendVoiceMailFragment$3.onClick(SendVoiceMailFragment.java:59)
11-28 15:53:54.897: W/System.err(5935):     at android.view.View.performClick(View.java:2532)
11-28 15:53:54.897: W/System.err(5935):     at android.view.View$PerformClick.run(View.java:9293)
11-28 15:53:54.897: W/System.err(5935):     at android.os.Handler.handleCallback(Handler.java:587)
11-28 15:53:54.897: W/System.err(5935):     at android.os.Handler.dispatchMessage(Handler.java:92)
11-28 15:53:54.897: W/System.err(5935):     at android.os.Looper.loop(Looper.java:150)
11-28 15:53:54.897: W/System.err(5935):     at android.app.ActivityThread.main(ActivityThread.java:4277)
11-28 15:53:54.897: W/System.err(5935):     at java.lang.reflect.Method.invokeNative(Native Method)
11-28 15:53:54.897: W/System.err(5935):     at java.lang.reflect.Method.invoke(Method.java:507)
11-28 15:53:54.897: W/System.err(5935):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-28 15:53:54.897: W/System.err(5935):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-28 15:53:54.897: W/System.err(5935):     at dalvik.system.NativeStart.main(Native Method)

编辑 这是我的清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.abc"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" /> 

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" >
    </uses-permission>
    <uses-permission
        android:name="android.permission.INTERNET"
        android:protectionLevel="normal" >
    </uses-permission>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" >
    </uses-permission>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" >
    </uses-permission>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" >
    </uses-permission>
    <uses-permission android:name="android.permission.GET_ACCOUNTS" >
    </uses-permission>
    <uses-permission android:name="android.permission.USE_CREDENTIALS" >
    </uses-permission>
    <uses-permission android:name="android.permission.MANAGE_ACCOUNTS" >
    </uses-permission>
    <uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" >
    </uses-permission>
    <uses-permission android:name="android.permission.CAMERA" >
    </uses-permission>
    <uses-permission android:name="android.permission.SEND_SMS" >
    </uses-permission>
    <uses-permission android:name="android.permission.RECORD_AUDIO" >
    </uses-permission>
    <uses-permission android:name="androd.permission.WRITE_EXTERNAL_STORAGE" />

    <supports-screens
        android:anyDensity="true"
        android:largeScreens="false"
        android:normalScreens="true"
        android:smallScreens="true" />

    <application
        android:debuggable="true"
        android:icon="@drawable/icon"
        android:label="@string/app_name"
        android:theme="@style/tab_name" >
        <activity
            android:name=".SplashScreen"
            android:icon="@drawable/icon"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.NoTitleBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>    
</manifest>

我创建文件的代码

private String getFilename(){
        String filepath = Environment.getExternalStorageDirectory().getPath();
        File file = new File(filepath,AUDIO_RECORDER_FOLDER);

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

        return (file.getAbsolutePath() + "/" + AUDIO_FILE_NAME + ".3gp");
    }
4

2 回答 2

0

您是否创建了存储文件的文件夹?(使用mkdirs()

于 2012-11-28T11:41:48.617 回答
0

在清单中执行您正在执行的操作的正确方法是:

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" /> <!-- You need a target

这部分也应该在<uses-sdk>标签之后:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
于 2012-11-28T14:35:45.387 回答