上传Photo时出现Android FileNotFoundException

时间:2014-04-24 08:27:27

标签: android

我在保存后找到照片时遇到问题。 这是我如何创建Intent从相机拍摄照片并将filePath保存到它。

private void takePhoto(int position)
{
    Meter meter = adapter.getItem(position);
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    File tempFile = createTempFile(meter.id, JPEG_FILE_SUFFIX, storageDir);
    if(tempFile != null)
    {
        lastPhotoPath = tempFile.getAbsolutePath();
        Log.d(TAG, "temp picture path=" + lastPhotoPath);
        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
            Uri.fromFile(tempFile));
        try
        {
            startActivityForResult(takePictureIntent, TAKE_PHOTO_REQUEST);
            lastPhotoPosition = position;
        }
        catch(ActivityNotFoundException exc)
        {
            Log.e(TAG, "activity to take photo not found");
        }
    }
}

然后我想将此图像上传到服务器。 我是这样做的。

public void compose(OutputStream out) throws DataStorageException
{
    Log.d("MainMenuActivity", "file not found in path " + path);
    InputStream in = null;
    try
    {
        in = new BufferedInputStream(new FileInputStream(path));

        // TODO: there is a better way
        byte[] buf = new byte[8192];
        while(true)
        {
            int length = in.read(buf);
            if(length < 0)
                break;
            out.write(buf, 0, length);
        }
    }
    catch(FileNotFoundException exc)
    {
        throw new DataStorageInternalErrorException("FileNotFoundException, FileRequestComposer compose");
    }
    catch(IOException exc)
    {
        // TODO: probably network error
        throw new DataStorageInternalErrorException("IOException, FileRequestComposer compose");
    }
    finally
    {
        if(in != null)
        {
            try
            {
                in.close();
            }
            catch(IOException exc)
            {
                // FIXME
            }
        }
    }
}

我从save和filePath检查filePath以保存它们完全相同:

temp picture path=/mnt/sdcard/Pictures/19520983731349.jpg

file not found in path /mnt/sdcard/Pictures/19520983731349.jpg

方法compose()

中抛出了异常

FileNotFoundException, FileRequestComposer compose

我做错了什么想法?

P.S。我无法在/ mnt / sdcard / Pictures mb中看到此文件。他迷路了还是像这样?请提出任何想法。

P.P.S Manifest

<?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.vodomer"
    android:versionCode="1"
    android:versionName="1.0">
    <uses-sdk android:minSdkVersion="7" 
              android:targetSdkVersion="19"/>
    <uses-feature android:name="android.hardware.camera" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <application android:label="@string/app_name"
                 android:name="com.vodomer.DatabaseApplication" >
        <activity android:name="Vodomer"
            android:label="@string/app_name"
            android:icon="@drawable/icon"
            android:theme="@android:style/Theme.NoTitleBar"
            android:screenOrientation="portrait"
            >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".activity.AddressesListActivity"
            android:theme="@android:style/Theme.NoTitleBar"
            android:screenOrientation="portrait"
            >
        </activity>
        <activity android:name=".activity.MainMenuActivity"
            android:theme="@android:style/Theme.NoTitleBar"
            android:screenOrientation="portrait"
            >
        </activity>
        <activity android:name=".activity.MetersActivity"
            android:theme="@android:style/Theme.NoTitleBar"
            android:windowSoftInputMode="adjustPan"
            android:screenOrientation="portrait"
            >
        </activity>
        <activity android:name=".activity.PersonalAccountActivity"
            android:theme="@android:style/Theme.NoTitleBar"
            android:screenOrientation="portrait"
            >
        </activity>
    </application>
</manifest>

2 个答案:

答案 0 :(得分:0)

问题可能在于您创建文件的方式。而不是createTempFile尝试常规构造函数:

File tempFile = new File(storageDir,  meter.id + JPEG_FILE_SUFFIX);

通常,您还需要确保您的storageDir文件夹存在,并根据需要使用mkdirs创建。

答案 1 :(得分:0)

添加此路径以在sdcard上存储图像。

final String uploadFilePath = "/mnt/sdcard/";