从openFileOutput发生NullPointerException。 context.getPackageName()= android,这段代码是框架。 我不知道这个问题,背景或任何事情...... 有人可以帮帮我吗?
- 使用联系人应用
02-16 02:06:42.449:I / VCardImport(2255):vCard缓存线程开始运行。
02-16 02:06:42.449:E / VCardImport(2255):filename:import_tmp_0.vcf
02-16 02:06:42.449:E / VCardImport(2255):file:/data/data/com.android.contacts/files/import_tmp_0.vcf
- 我的来源,框架
02-16 01:11:00.525:E / PstContactCreator(549):filename:import_tmp_0.vcf
02-16 01:11:00.525:W / ApplicationContext(549):无法创建文件目录文件
02-16 01:11:00.525:E / PstContactCreator(549):file:import_tmp_0.vcf
我的源代码是' android',当我使用的函数是getFileStreamPath,openFileOutput时,我无法创建文件。我认为上下文会导致问题。我想知道导致问题的原因以及如何解决问题。 context.getFileStreamPath(文件名); context.openFileOutput(filename,Context.MODE_PRIVATE).getChannel();
private Uri copyTo(final Uri sourceUri,String filename)抛出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 start
02-16 01:11:00.455:I / PstManagerService(549):f.exists
02-16 01:11:00.455:I / PstManagerService(549):在creteNewFile之后
02-16 01:11:00.465:W / PstContactCreator(549):pst联系人开始!
02-16 01:11:00.465:E / PstContactCreator(549):filename:import_tmp_0.vcf
02-16 01:11:00.465:W / ApplicationContext(549):无法创建文件目录文件
02-16 01:11:00.475:E / PstContactCreator(549):file:import_tmp_0.vcf
02-16 01:11:00.475:I / PstContactCreator(549):将Uri复制到应用本地存储 (file:///cache/pst_contact_r.tmp - > import_tmp_0.vcf)
02-16 01:11:00.475:I / PstContactCreator(549):dcshin 111
02-16 01:11:00.475:E / PstContactCreator(549):inputChannel: 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_tmp_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:null
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 start
02-16 01:11:00.515:I / PstManagerService(549):f.exists
02-16 01:11:00.515:I / PstManagerService(549):在creteNewFile之后
02-16 01:11:00.525:W / PstContactCreator(549):联系开始!
02-16 01:11:00.525:E / PstContactCreator(549):filename:import_tmp_0.vcf
02-16 01:11:00.525:W / ApplicationContext(549):无法创建文件目录文件
02-16 01:11:00.525:E / PstContactCreator(549):file:import_tmp_0.vcf
02-16 01:11:00.525:I / PstContactCreator(549):将Uri复制到应用本地存储 (file:///cache/pst_contact_r.tmp - > import_tmp_0.vcf)
02-16 01:11:00.525:I / PstContactCreator(549):dcshin 111
02-16 01:11:00.525:E / PstContactCreator(549):inputChannel: 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_tmp_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 start
02-16 01:11:00.535:I / PstManagerService(549):f.exists
02-16 01:11:00.535:I / PstManagerService(549):在creteNewFile之后
02-16 01:11:00.535:W / PstContactCreator(549):pst联系开始!
02-16 01:11:00.535:E / PstContactCreator(549):filename:import_tmp_0.vcf
02-16 01:11:00.535:W / ApplicationContext(549):无法创建文件目录文件
02-16 01:11:00.535:E / PstContactCreator(549):file:import_tmp_0.vcf
02-16 01:11:00.535:I / PstContactCreator(549):将Uri复制到应用本地存储 (file:///cache/pst_contact_r.tmp - > import_tmp_0.vcf)
02-16 01:11:00.535:I / PstContactCreator(549):dcshin 111
02-16 01:11:00.535:E / PstContactCreator(549):inputChannel: 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_tmp_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:null
02-16 01:11:00.545:E / PstContactCreator(549):getPackageResourcePath:null
02-16 01:11:00.545:W / ApplicationContext(549):无法创建文件目录文件
02-16 01:11:00.545:E / PstContactCreator(549):java.lang.NullPointerException
02-16 01:11:00.545:I / PstContactCreator(549):已完成缓存vCard。
02-16 01:11:00.545:I / PstManagerService(549):onsetrecordcmd start
02-16 01:11:00.545:I / PstManagerService(549):f.exists
02-16 01:11:00.555:I / PstManagerService(549):在creteNewFile之后
02-16 01:11:00.555:W / PstContactCreator(549):pst联系开始!
02-16 01:11:00.555:E / PstContactCreator(549):filename:import_tmp_0.vcf
02-16 01:11:00.555:W / ApplicationContext(549):无法创建文件目录文件
02-16 01:11:00.555:E / PstContactCreator(549):file:import_tmp_0.vcf
02-16 01:11:00.555:I / PstContactCreator(549):将Uri复制到应用本地存储 (file:///cache/pst_contact_r.tmp - > import_tmp_0.vcf)
02-16 01:11:00.555:I / PstContactCreator(549):dcshin 111
02-16 01:11:00.555:E / PstContactCreator(549):inputChannel: java.nio.channels.Channels$InputStreamChannel@41a20ff8
02-16 01:11:00.555:I / PstContactCreator(549):dcshin 222
02-16 01:11:00.555:W / ApplicationContext(549):无法创建文件目录文件
02-16 01:11:00.555:E / PstContactCreator(549):destUri:file:/import_tmp_0.vcf
02-16 01:11:00.555:I / PstContactCreator(549):dcshin 333
02-16 01:11:00.555:E / PstContactCreator(549):getPackageName:android
02-16 01:11:00.555:E / PstContactCreator(549):getPackageCodePath:null
02-16 01:11:00.555:E / PstContactCreator(549):getPackageResourcePath:null
02-16 01:11:00.555:W / ApplicationContext(549):无法创建文件目录文件
02-16 01:11:00.555:E / PstContactCreator(549):java.lang.NullPointerException
02-16 01:11:00.555:I / PstContactCreator(549):已完成缓存vCard。
02-16 01:11:00.565:I / DiagJNIInterface(1791):要发送消息
02-16 01:11:00.565:D / ShutdownThread(549):通知线程开始关机 longPressBehavior = 1 02-16 01:11:00.615:I / ShutdownThread(549):发送关闭广播......
答案 0 :(得分:0)
首先检查你的Android清单文件。在权限中,您应该添加WRITE_EXTERNAL:storage 或写入联系人的权限。我看到你正在处理
答案 1 :(得分:0)
您的应用的包名是android
?那是错的。将其更改为com.yourname
。
答案 2 :(得分:0)
我有一个类似的问题(NullPointerException看起来来自openFileOutput),我在那里创建了一个java类而没有先传入Context。
getApplicationContext().openFileOutput(FILENAME, Context.MODE_PRIVATE);
没有用,(即使编译好了)所以我把它改成了下面的。 (注意ctx.openFileOutput。
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();
}
}
虽然这可能无法解决您的问题,但我会将其放在此处,以防其他人帮助您。