自定义内容提供商 - 保存图像

时间:2011-10-12 14:03:17

标签: android android-contentprovider

我一直在获取FileNotFoundException / IOException,

File directory = new File(Environment.getExternalStorageDirectory()
            .toString()
            + "/Android/data/<package>");

    if (!directory.exists()) {
        boolean x = directory.mkdirs();
        Log.d(TAG, "directory: " + x);
    } 

    File path = new File(directory.getPath() , uri.getEncodedPath());


    int imode = 0;
    if (mode.contains("w")) {
        imode |= ParcelFileDescriptor.MODE_READ_WRITE;
        if (!path.exists()) {

            try {

            path.createNewFile();



            } catch (IOException e) {

                e.printStackTrace();
                Log.d(TAG, "Error " + e.getMessage());

            }
        }
    }
    if (mode.contains("r"))
        imode |= ParcelFileDescriptor.MODE_READ_ONLY;
    if (mode.contains("+"))
        imode |= ParcelFileDescriptor.MODE_APPEND;

    return ParcelFileDescriptor.open(path, imode);

例外是

10-12 15:01:10.360: WARN/System.err(18679): java.io.IOException: No such file or directory

10-12 15:01:10.360:WARN / System.err(18679):at java.io.File.createNewFileImpl(Native Method) 10-12 15:01:10.360:WARN / System.err(18679):at java.io.File.createNewFile(File.java:1115) 10-12 15:01:10.360:WARN / System.err(18679):at com.hair.android.camera.ImageContentProvider.openFile(ImageContentProvider.java:230) 10-12 15:01:10.360:WARN / System.err(18679):在android.content.ContentProvider.openAssetFile(ContentProvider.java:726) 10-12 15:01:10.360:WARN / System.err(18679):在android.content.ContentProvider $ Transport.openAssetFile(ContentProvider.java:247) 10-12 15:01:10.360:WARN / System.err(18679):在android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:534) 10-12 15:01:10.360:WARN / System.err(18679):在android.content.ContentResolver.openOutputStream(ContentResolver.java:402) 10-12 15:01:10.360:WARN / System.err(18679):在android.content.ContentResolver.openOutputStream(ContentResolver.java:378) 10-12 15:01:10.360:WARN / System.err(18679):at com.hair.android.HairAppActivity.onActivityResult(HairAppActivity.java:428) 10-12 15:01:10.360:WARN / System.err(18679):在android.app.Activity.dispatchActivityResult(Activity.java:4541) 10-12 15:01:10.360:WARN / System.err(18679):在android.app.ActivityThread.deliverResults(ActivityThread.java:2740) 10-12 15:01:10.360:WARN / System.err(18679):在android.app.ActivityThread.handleSendResult(ActivityThread.java:2787) 10-12 15:01:10.360:WARN / System.err(18679):在android.app.ActivityThread.access $ 2000(ActivityThread.java:122) 10-12 15:01:10.360:WARN / System.err(18679):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1032) 10-12 15:01:10.360:WARN / System.err(18679):在android.os.Handler.dispatchMessage(Handler.java:99) 10-12 15:01:10.360:WARN / System.err(18679):在android.os.Looper.loop(Looper.java:132) 10-12 15:01:10.360:WARN / System.err(18679):在android.app.ActivityThread.main(ActivityThread.java:4025) 10-12 15:01:10.360:WARN / System.err(18679):at java.lang.reflect.Method.invokeNative(Native Method) 10-12 15:01:10.360:WARN / System.err(18679):at java.lang.reflect.Method.invoke(Method.java:491) 10-12 15:01:10.360:WARN / System.err(18679):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:841) 10-12 15:01:10.360:WARN / System.err(18679):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599) 10-12 15:01:10.360:WARN / System.err(18679):at dalvik.system.NativeStart.main(Native Method)

我有什么想法可以解决这个问题?提前谢谢

1 个答案:

答案 0 :(得分:0)

new File(Environment.getExternalStorageDirectory()
            .toString() 

这不正确 - 使用Environment.getExternalStorageDirectory()。getAbsolutePath()