您正在我的应用程序中获取联系人图像,然后应用程序正在运行但是在这样的cat日志中出现错误。如何解决这个问题..请看下面的代码帮助我
at java.io.BufferedInputStream.streamClosed(BufferedInputStream.java:118) 在java.io.BufferedInputStream.read(BufferedInputStream.java:279)
loading image
--------------------
private Bitmap getContactImage(String mNumber) {
Bitmap cPhoto = null;
BufferedInputStream buf_stream = null;
Cursor cursorPhoto = getActivity().getContentResolver().query(
Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
Uri.decode(mNumber)), new String[] { PhoneLookup._ID },
null, null, null);
InputStream photo_stream = null;
if (cursorPhoto.moveToFirst()) {
String contactId = cursorPhoto.getString(0);
Uri contactPhotoUri = ContentUris.withAppendedId(
ContactsContract.Contacts.CONTENT_URI,
Long.parseLong(contactId));
try {
photo_stream = ContactsContract.Contacts
.openContactPhotoInputStream(getActivity()
.getContentResolver(), contactPhotoUri);
buf_stream = new BufferedInputStream(photo_stream);
cPhoto = BitmapFactory.decodeStream(buf_stream);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (buf_stream != null && photo_stream != null) {
try {
buf_stream.close();
photo_stream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
error in cat log
------------------------
GC_CONCURRENT freed 183K, 4% free 7135K/7431K, paused 8ms+8ms
05-31 10:08:51.518: W/System.err(968): java.io.IOException: BufferedInputStream is closed
05-31 10:08:51.538: W/System.err(968): at java.io.BufferedInputStream.streamClosed(BufferedInputStream.java:118)
05-31 10:08:51.549: W/System.err(968): at java.io.BufferedInputStream.read(BufferedInputStream.java:279)
05-31 10:08:51.569: W/System.err(968): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
05-31 10:08:51.569: W/System.err(968): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:493)
05-31 10:08:51.569: W/System.err(968): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:549)
05-31 10:08:51.569: W/System.err(968): at com.example.demologs.CallLogsFragment.getContactImage(CallLogsFragment.java:195)
05-31 10:08:51.589: W/System.err(968): at com.example.demologs.CallLogsFragment.readCallLogs(CallLogsFragment.java:126)
05-31 10:08:51.589: W/System.err(968): at com.example.demologs.CallLogsFragment.onStart(CallLogsFragment.java:53)
05-31 10:08:51.599: W/System.err(968): at android.support.v4.app.Fragment.performStart(Fragment.java:1484)
05-31 10:08:51.610: W/System.err(968): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:941)
05-31 10:08:51.618: W/System.err(968): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
05-31 10:08:51.618: W/System.err(968): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1070)
05-31 10:08:51.629: W/System.err(968): at android.support.v4.app.FragmentManagerImpl.dispatchStart(FragmentManager.java:1866)
05-31 10:08:51.638: W/System.err(968): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:568)
05-31 10:08:51.638: W/System.err(968): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1133)
05-31 10:08:51.649: W/System.err(968): at android.app.Activity.performStart(Activity.java:4475)
05-31 10:08:51.649: W/System.err(968): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1928)
05-31 10:08:51.668: W/System.err(968): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
05-31 10:08:51.668: W/System.err(968): at android.app.ActivityThread.access$600(ActivityThread.java:122)
05-31 10:08:51.698: W/System.err(968): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
05-31 10:08:51.708: W/System.err(968): at android.os.Handler.dispatchMessage(Handler.java:99)
05-31 10:08:51.708: W/System.err(968): at android.os.Looper.loop(Looper.java:137)
05-31 10:08:51.708: W/System.err(968): at android.app.ActivityThread.main(ActivityThread.java:4340)
05-31 10:08:51.719: W/System.err(968): at java.lang.reflect.Method.invokeNative(Native Method)
05-31 10:08:51.719: W/System.err(968): at java.lang.reflect.Method.invoke(Method.java:511)
05-31 10:08:51.738: W/System.err(968): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-31 10:08:51.738: W/System.err(968): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-31 10:08:51.748: W/System.err(968): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
如果联系人中没有照片,则返回的InputStream
为空。在尝试解码图像之前,您应该检查photo_stream
是否为空。
try {
photo_stream = ContactsContract.Contacts
.openContactPhotoInputStream(getActivity()
.getContentResolver(), contactPhotoUri);
if(photo_stream != null) {
buf_stream = new BufferedInputStream(photo_stream);
cPhoto = BitmapFactory.decodeStream(buf_stream);
}
} catch (Exception e) {
e.printStackTrace();
}