Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/183.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在openFileOutput Android中,NullPointerException_Android - Fatal编程技术网

在openFileOutput Android中,NullPointerException

在openFileOutput Android中,NullPointerException,android,Android,从openFileOutput发生NullPointerException。 context.getPackageName()=android,此代码是框架。 我不知道这个问题,背景或任何东西。。。 有人能帮我吗 -使用“联系人”应用程序 02-16 02:06:42.449:I/VCardImport(2255):vCard缓存线程开始运行 02-16 02:06:42.449:E/VCardImport(2255):文件名:import_tmp_0.vcf 02-16 02:06:42.44

从openFileOutput发生NullPointerException。 context.getPackageName()=android,此代码是框架。 我不知道这个问题,背景或任何东西。。。 有人能帮我吗

-使用“联系人”应用程序

02-16 02:06:42.449:I/VCardImport(2255):vCard缓存线程开始运行

02-16 02:06:42.449:E/VCardImport(2255):文件名:import_tmp_0.vcf

02-16 02:06:42.449:E/VCardImport(2255):文件:/data/data/com.android.contacts/files/import\u tmp\u 0.vcf

-我的来源、框架

02-16 01:11:00.525:E/PstContactCreator(549):文件名:import_tmp_0.vcf

02-16 01:11:00.525:W/ApplicationContext(549):无法创建文件目录文件

02-16 01:11:00.525:E/PstContactCreator(549):文件:import_tmp_0.vcf

我的源代码的上下文是“android”,当我使用getFileStreamPath、openFileOutput等函数时,我无法创建文件。我认为是环境造成的问题。我想知道是什么导致了问题,以及如何解决问题。 getFileStreamPath(文件名); context.openFileOutput(文件名,context.MODE_PRIVATE).getChannel()

私有Uri copyTo(最终Uri sourceUri,字符串文件名)引发IOException{

    Log.i(LOG_TAG, String.format("Copy a Uri to app local storage (%s -> %s)",
            sourceUri, filename));
    final Context context = mContext.get();
    final ContentResolver resolver = context.getContentResolver();
    ReadableByteChannel inputChannel = null;
    WritableByteChannel outputChannel = null;
    Uri destUri = null;
    try {
        inputChannel = Channels.newChannel(resolver.openInputStream(sourceUri));
        destUri = Uri.parse(context.getFileStreamPath(filename).toURI().toString());
        outputChannel = context.**openFileOutput**(filename, Context.MODE_PRIVATE).getChannel();

        final ByteBuffer buffer = ByteBuffer.allocateDirect(8192);

        while (inputChannel.read(buffer) != -1) {
            buffer.flip();
            outputChannel.write(buffer);
            buffer.compact();
        }

        buffer.flip();

        while (buffer.hasRemaining()) {
            outputChannel.write(buffer);
        }

    } finally {
        if (inputChannel != null) {
            try {
                inputChannel.close();
            } catch (IOException e) {
                Log.w(LOG_TAG, "Failed to close inputChannel.");
            }
        }
        if (outputChannel != null) {
            try {
                outputChannel.close();
            } catch(IOException e) {
                Log.w(LOG_TAG, "Failed to close outputChannel");
            }
        }
    }
    return destUri;
}
02-16 01:11:00.455:I/PstManagerService(549):onsetrecordcmd启动

02-16 01:11:00.455:I/PstManagerService(549):f

02-16 01:11:00.455:I/PstManagerService(549):在CreteneFile之后

02-16 01:11:00.465:W/pst联系人创建者(549):pst联系人开始

02-16 01:11:00.465:E/PstContactCreator(549):文件名:import\u tmp\u 0.vcf

02-16 01:11:00.465:W/ApplicationContext(549):无法创建文件目录文件

02-16 01:11:00.475:E/PstContactCreator(549):文件:import_tmp_0.vcf

02-16 01:11:00.475:I/PstContactCreator(549):将Uri复制到应用程序本地存储 (file:///cache/pst_contact_r.tmp ->导入(tmp_0.vcf)

02-16 01:11:00.475:I/PstContactCreator(549):dcshin 111

02-16 01:11:00.475:E/PstContactCreator(549):输入通道: java.nio.channels.channels$InputStreamChannel@419ca7a8

02-16 01:11:00.475:I/PstContactCreator(549):dcshin 222

02-16 01:11:00.485:W/ApplicationContext(549):无法创建文件目录文件

02-16 01:11:00.495:E/PstContactCreator(549):destUri:file:/import\u tmp\u 0.vcf

02-16 01:11:00.495:I/PstContactCreator(549):dcshin 333

02-16 01:11:00.495:E/PstContactCreator(549):getPackageName:android

02-16 01:11:00.495:E/PstContactCreator(549):getPackageCodePath:空

02-16 01:11:00.505:E/PstContactCreator(549):getPackageResourcePath:null

02-16 01:11:00.505:W/ApplicationContext(549):无法创建文件目录文件

02-16 01:11:00.505:E/PstContactCreator(549):java.lang.NullPointerException

02-16 01:11:00.505:I/PstContactCreator(549):已完成缓存vCard

02-16 01:11:00.515:I/PstManagerService(549):onsetrecordcmd启动

02-16 01:11:00.515:I/PstManagerService(549):f

02-16 01:11:00.515:I/PstManagerService(549):在CreteneFile之后

02-16 01:11:00.525:W/pst联系人创建者(549):pst联系人开始

02-16 01:11:00.525:E/PstContactCreator(549):文件名:import_tmp_0.vcf

02-16 01:11:00.525:W/ApplicationContext(549):无法创建文件目录文件

02-16 01:11:00.525:E/PstContactCreator(549):文件:import_tmp_0.vcf

02-16 01:11:00.525:I/PstContactCreator(549):将Uri复制到应用程序本地存储 (file:///cache/pst_contact_r.tmp ->导入(tmp_0.vcf)

02-16 01:11:00.525:I/PstContactCreator(549):dcshin 111

02-16 01:11:00.525:E/PstContactCreator(549):输入通道: java.nio.channels.channels$InputStreamChannel@419b0018

02-16 01:11:00.525:I/PstContactCreator(549):dcshin 222

02-16 01:11:00.525:W/ApplicationContext(549):无法创建文件目录文件

02-16 01:11:00.525:E/ThermalDaemon(2916):最大CPU[1]频率918000 KHz

02-16 01:11:00.525:E/PstContactCreator(549):destUri:file:/import\u tmp\u 0.vcf

02-16 01:11:00.525:I/PstContactCreator(549):dcshin 333

02-16 01:11:00.525:E/PstContactCreator(549):getPackageName:android

02-16 01:11:00.525:E/PstContactCreator(549):getPackageCodePath:null

02-16 01:11:00.525:E/PstContactCreator(549):getPackageResourcePath:null

02-16 01:11:00.525:W/ApplicationContext(549):无法创建文件目录文件

02-16 01:11:00.525:E/PstContactCreator(549):java.lang.NullPointerException

02-16 01:11:00.525:I/PstContactCreator(549):已完成缓存vCard

02-16 01:11:00.535:I/PstManagerService(549):onsetrecordcmd启动

02-16 01:11:00.535:I/PstManagerService(549):f

02-16 01:11:00.535:I/PstManagerService(549):在CreteneFile之后

02-16 01:11:00.535:W/pst联系人创建者(549):pst联系人开始

02-16 01:11:00.535:E/PstContactCreator(549):文件名:import\u tmp\u 0.vcf

02-16 01:11:00.535:W/ApplicationContext(549):无法创建文件目录文件

02-16 01:11:00.535:E/PstContactCreator(549):文件:导入\u tmp\u 0.vcf

02-16 01:11:00.535:I/PstContactCreator(549):将Uri复制到应用程序本地存储 (file:///cache/pst_contact_r.tmp ->导入(tmp_0.vcf)

02-16 01:11:00.535:I/PstContactCreator(549):dcshin 111

02-16 01:11:00.535:E/PstContactCreator(549):输入通道: java.nio.channels.channels$InputStreamChannel@414d6ca8

02-16 01:11:00.535:I/PstContactCreator(549):dcshin 222

02-16 01:11:00.535:W/ApplicationContext(549):无法创建文件目录文件

02-16 01:11:00.545:E/PstContactCreator(549):destUri:file:/import\u tmp\u 0.vcf

02-16 01:11:00.545:I/PstContactCreator(549):dcshin 333

02-16 01:11:00.545:E/PstContactCreator(549):getPackageName:android

02-16 01:11:00.545:E/PstContactCreator(549):getPackageCodePath:空

02-16 01:11:00.545:E/p
getApplicationContext().openFileOutput(FILENAME, Context.MODE_PRIVATE);
public ClassName(Context ctx){
    FileOutputStream fos;
    try {
        fos = ctx.openFileOutput(FILENAME, Context.MODE_PRIVATE);
        fos.write(("Date created: " + SimpleDateFormat.getDateInstance().format(now) + "\n").getBytes());
        fos.close();
    }
    catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    catch (IOException e) {
        e.printStackTrace();
    }
}