gpt4 book ai didi

android - 制作 Vcard 时在 Android ICS 中出现错误

转载 作者:搜寻专家 更新时间:2023-11-01 09:01:06 24 4
gpt4 key购买 nike

在 android 2.1 中运行良好

代码

String vfile ="POContactsRestore.vcf";
String VCard = null;
File vfile = new File("/sdcard/POContactsRestore.vcf");
if(vfile.exists()){
vfile.delete();
}
FileOutputStream mFileOutputStream = null;
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null, null, null);

phones.moveToFirst();
for(int i =0;i<phones.getCount();i++)
{
String lookupKey = phones.getString(phones.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey);

AssetFileDescriptor fd;
try
{
fd = getContentResolver().openAssetFileDescriptor(uri, "r");
FileInputStream fis = fd.createInputStream();
byte[] buf = new byte[(int) fd.getDeclaredLength()];
fis.read(buf);
VCard = new String(buf);
String path = Environment.getExternalStorageDirectory().toString() + File.separator + vfile;
mFileOutputStream = new FileOutputStream(path, false);

phones.moveToNext();
Log.d("Vcard", VCard);
}
catch (Exception e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}

}

try {
mFileOutputStream.write(VCard.toString().getBytes());
mFileOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

logcat 错误

      02-14 06:03:26.724: E/AndroidRuntime(862): java.lang.NullPointerException
02-14 06:03:26.724: E/AndroidRuntime(862): at com.example.com.example.contactbackup.Contact$1.onClick(Contact.java:148)
02-14 06:55:38.183: E/AndroidRuntime(1066): FATAL EXCEPTION: main
02-14 06:55:38.183: E/AndroidRuntime(1066): java.lang.NullPointerException
02-14 06:55:38.183: E/AndroidRuntime(1066): at com.example.com.anthem.contactbackup.Contact$1.onClick(Contact.java:101)
02-14 06:55:38.183: E/AndroidRuntime(1066): at android.view.View.performClick(View.java:4084)
02-14 06:55:38.183: E/AndroidRuntime(1066): at android.view.View$PerformClick.run(View.java:16966)
02-14 06:55:38.183: E/AndroidRuntime(1066): at android.os.Handler.handleCallback(Handler.java:615)
02-14 06:55:38.183: E/AndroidRuntime(1066): at android.os.Handler.dispatchMessage(Handler.java:92)
02-14 06:55:38.183: E/AndroidRuntime(1066): at android.os.Looper.loop(Looper.java:137)
02-14 06:55:38.183: E/AndroidRuntime(1066): at android.app.ActivityThread.main(ActivityThread.java:4745)
02-14 06:55:38.183: E/AndroidRuntime(1066): at java.lang.reflect.Method.invokeNative(Native Method)
02-14 06:55:38.183: E/AndroidRuntime(1066): at java.lang.reflect.Method.invoke(Method.java:511)
02-14 06:55:38.183: E/AndroidRuntime(1066): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
02-14 06:55:38.183: E/AndroidRuntime(1066): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-14 06:55:38.183: E/AndroidRuntime(1066): at dalvik.system.NativeStart.main(Native Method)

最佳答案

试试这个,问题出在FileInputStream fis = fd.createInputStream();

package com.example.temp;

import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.ContactsContract;
import android.util.Log;

public class MainActivity extends Activity {
Cursor cursor;
ArrayList<String> vCard;
String vfile;
static Context mContext;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = MainActivity.this;
getVCF();
}

public static void getVCF() {
final String vfile = "POContactsRestore.vcf";
Cursor phones = mContext.getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
phones.moveToFirst();
for (int i = 0; i < phones.getCount(); i++) {
String lookupKey = phones.getString(phones.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey);
AssetFileDescriptor fd;
try {
fd = mContext.getContentResolver().openAssetFileDescriptor(uri, "r");
FileDescriptor fdd = fd.getFileDescriptor();
InputStream in = new FileInputStream(fdd);
//FileInputStream fis = fd.createInputStream();
Log.d("BLA BLA", "Len is :: " + (int) fd.getDeclaredLength());
byte[] buf = new byte[(int) fd.getDeclaredLength()];
in.read(buf);
fd.close();
String VCard = new String(buf);
String path = Environment.getExternalStorageDirectory().toString() + File.separator + vfile;
FileOutputStream mFileOutputStream = new FileOutputStream(path, true);
mFileOutputStream.write(VCard.toString().getBytes());
mFileOutputStream.close();
in.close();
phones.moveToNext();
Log.d("Vcard", VCard);

} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}

关于android - 制作 Vcard 时在 Android ICS 中出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14869124/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com