gpt4 book ai didi

android - HTC 10,ExifInterface的ANR

转载 作者:行者123 更新时间:2023-12-03 16:05:43 28 4
gpt4 key购买 nike

我在使用HTC 10(htc_pmeuhljapan),Android 7.0的游戏商店中收到以下错误消息。

Input dispatching timed out (Waiting to send non-key event because the touched window has not finished processing certain input events that were delivered to it over 500.0ms ago. Wait queue length: 25. Wait queue head age: 15678.7ms.)



根据 this link,我知道这是ANR。但是我无法重现这一点。

代码如下:
 private void refreshListCameraRoll()
{
String[] projection;
Uri uri;
Cursor cur;
projection = new String[]{
MediaStore.Images.Media._ID,
MediaStore.Images.Media.DATA,
MediaStore.Images.Media.DATE_ADDED
};

uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
cur = getContext().getContentResolver().query(uri,
projection, // Which columns to return
null, // Which rows to return (all rows)
null, // Selection arguments (none)
MediaStore.Images.Media.DATE_ADDED + " DESC"
);
Log.i("ListingImages", " query count=" + cur.getCount());
showProgress("Loading...");

if (cur.moveToFirst()) {
Long id;
String path;
long dateS;

int idColumn = cur.getColumnIndex(MediaStore.Images.Media._ID);
int pathColumn = cur.getColumnIndex(MediaStore.Images.Media.DATA);

int dateSColumn =
cur.getColumnIndex(MediaStore.Images.Media.DATE_ADDED);

do {
id = cur.getLong(idColumn);
path = cur.getString(pathColumn);
dateS = cur.getLong(dateSColumn);
long dateMS = dateS / S_IN_A_DAY * MS_IN_A_DAY;
long differenceDates = 0;
try {
Date currentDate = new Date();
Date oldDate = new Date(dateMS);
long difference = Math.abs(currentDate.getTime() - oldDate.getTime());
differenceDates = difference / (24 * 60 * 60 * 1000);
} catch(Exception e) {
e.printStackTrace();
}
if(differenceDates <= 31) {
ExifInterface intf = null;
try {
intf = new ExifInterface(path);
} catch (IOException e) {
e.printStackTrace();
}
if (intf != null) {
String dateString = intf.getAttribute(ExifInterface.TAG_DATETIME);
if (dateString != null) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");
try {
Date date = sdf.parse(dateString);
dateMS = date.getTime() + MS_IN_A_DAY;
} catch (Exception e) {
e.printStackTrace();
}
}
}
Bundle bundle = new Bundle();
bundle.putBoolean(MEDIA_BUNDLE_KEY_ISVIDEO, false);
bundle.putLong(MEDIA_BUNDLE_KEY_ID, id);
bundle.putString(MEDIA_BUNDLE_KEY_PATH, path);
bundle.putLong(MEDIA_BUNDLE_KEY_DATE, dateMS);
mListMedia.add(bundle);
}
} while (cur.moveToNext());
}
cur.close();
projection = new String[]{
MediaStore.Video.Media._ID,
MediaStore.Video.Media.DATA,
MediaStore.Video.Media.DATE_ADDED
};


uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
cur = getContext().getContentResolver().query(uri,
projection, // Which columns to return
null, // Which rows to return (all rows)
null, // Selection arguments (none)
MediaStore.Video.Media.DATE_ADDED + " DESC" // Ordering
);

Log.i("ListingVideos", " query count=" + cur.getCount());

if (cur.moveToFirst()) {
Long id;
String path;
long dateS;

int idColumn = cur.getColumnIndex(MediaStore.Video.Media._ID);
int pathColumn = cur.getColumnIndex(MediaStore.Video.Media.DATA);
int dateSColumn = cur.getColumnIndex(MediaStore.Video.Media.DATE_ADDED);

do {
// Get the field values
id = cur.getLong(idColumn);
path = cur.getString(pathColumn);
dateS = cur.getInt(dateSColumn);

//filter media files for ONE month
long dateMS = dateS / S_IN_A_DAY * MS_IN_A_DAY;
long differenceDates = 0;
try {
SimpleDateFormat sd = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");
Date currentDate = new Date();
Date date = new Date(dateMS);
//Comparing dates
long difference = Math.abs(currentDate.getTime() - date.getTime());
differenceDates = difference / (24 * 60 * 60 * 1000);

} catch(Exception e) {
e.printStackTrace();
}
if(differenceDates <= 31) {
Bundle bundle = new Bundle();
bundle.putBoolean(MEDIA_BUNDLE_KEY_ISVIDEO, true);
bundle.putLong(MEDIA_BUNDLE_KEY_ID, id);
bundle.putString(MEDIA_BUNDLE_KEY_PATH, path);
bundle.putLong(MEDIA_BUNDLE_KEY_DATE, dateS * MS_IN_A_S);
mListMedia.add(bundle);

}
} while (cur.moveToNext());
}

hideProgress();
}

作为 android developer document,ExifInterface已在API级别24中添加,因此HTC 10,Android 7.0不会有问题。

以下是游戏商店的踪迹:编辑:
     "main" prio=5 tid=1 Runnable
| group="main" sCount=0 dsCount=0 obj=0x73eccab0 self=0xebc33400
| sysTid=12463 nice=0 cgrp=default sched=0/0 handle=0xebcf6534
| state=R schedstat=( 0 0 0 ) utm=765 stm=278 core=0 HZ=100
| stack=0xff197000-0xff199000 stackSize=8MB
| held mutexes= "mutator lock"(shared held)
at java.io.ByteArrayInputStream.read (ByteArrayInputStream.java)
at android.media.ExifInterface$ByteOrderAwarenessDataInputStream.readInt (ExifInterface.java:2636)
at android.media.ExifInterface$ByteOrderAwarenessDataInputStream.readUnsignedInt (ExifInterface.java:2677)
at android.media.ExifInterface.readImageFileDirectory (ExifInterface.java:2212)
at android.media.ExifInterface.readImageFileDirectory (ExifInterface.java:2262)
at android.media.ExifInterface.readExifSegment (ExifInterface.java:2093)
at android.media.ExifInterface.getJpegAttributes (ExifInterface.java:1892)
at android.media.ExifInterface.loadAttributes (ExifInterface.java:1474)
at android.media.ExifInterface.<init> (ExifInterface.java:1111)
at com.xxxxxxxx.xxxx.AlbumFragment.refreshListCameraRoll (AlbumFragment.java:815)
at com.xxxxxxxx.xxxx.AlbumFragment.refreshList (AlbumFragment.java:603)
at com.xxxxxxxx.xxxx.AlbumFragment.setAlbum (AlbumFragment.java:575)
at com.xxxxxxxx.xxxx.AlbumFragment.access$000 (AlbumFragment.java:103)
at com.xxxxxxxx.xxxx.AlbumFragment$1.onTabSelected (AlbumFragment.java:264)
at android.support.design.widget.TabLayout.selectTab (TabLayout.java:1025)
at android.support.design.widget.TabLayout.selectTab (TabLayout.java:995)
at android.support.design.widget.TabLayout$Tab.select (TabLayout.java:1272)
at android.support.design.widget.TabLayout$TabView.performClick (TabLayout.java:1377)
at android.view.View$PerformClick.run (View.java:22396)
at android.os.Handler.handleCallback (Handler.java:751)
at android.os.Handler.dispatchMessage (Handler.java:95)
at android.os.Looper.loop (Looper.java:173)
at android.app.ActivityThread.main (ActivityThread.java:6459)
at java.lang.reflect.Method.invoke! (Native method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:938)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:828)

确保AlbumFragment.java上的815行是
intf = new ExifInterface(path);

而且它崩溃了。任何想法对此表示高度赞赏。

最佳答案

ANR

In any situation in which your app performs a potentially lengthy operation, you should not perform the work on the UI thread, but instead create a worker thread and do most of the work there. This keeps the UI thread (which drives the user interface event loop) running and prevents the system from concluding that your code has frozen. Because such threading usually is accomplished at the class level, you can think of responsiveness as a class problem. (Compare this with basic code performance, which is a method-level concern.)



For more info

因此,如果我尝试观察您的代码,则您在UI线程中做了很多工作,这是引发ANR的主要原因。因此,您需要将代码放入 AsyncTask
private class RefreshListCameraRollAsyncTask extends AsyncTask<String, Void, String> {

@Override
protected String doInBackground(String... params) {
refreshListCameraRoll();
return "Executed";
}

@Override
protected void onPostExecute(String result) {
// do ur UI work here and hide loader
}

@Override
protected void onPreExecute() {
// show your loader here if you want to show
}


}

关于android - HTC 10,ExifInterface的ANR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48658069/

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