gpt4 book ai didi

java - 将图像加载到 ImageView 中会导致 "I/Choreographer: Skipped 139 frames! The application may be doing too much work on its main thread."错误

转载 作者:行者123 更新时间:2023-11-30 00:35:45 25 4
gpt4 key购买 nike

我正在使用相机 Intent 拍照,然后使用该图像将其显示在我的应用 fragment 中。图像将加载,但会导致大量性能问题,并导致应用程序在图像显示在屏幕上时运行非常糟糕。我试图实现线程来解决问题,但它仍在发生。在 logcat 中,我收到“I/Choreographer:跳过 175 帧!应用程序可能在其主线程上做了太多工作”警告。任何解决此问题的帮助将不胜感激。

我在我的 OnActivityResults 方法中执行线程,因为这是显示图像的地方。

Camera_fragment 类

public class Camera_Fragment extends Fragment {

private static final int REQUEST_IMAGE_CAPTURE = 1;
private static final int MY_PERMISSIONS_REQUEST_READ_CONTACTS = 0;
private ImageView imageView;

public Camera_Fragment() {
// Required empty public constructor
}

private String pictureImagePath = "";

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = timeStamp + ".png";

File mydir = getExternalStoragePublicDirectory("app image folder"); //Creating an internal dir;
if (!mydir.exists())
{

{
mydir.mkdirs();
}
}
isReadPermissionGranted();
pictureImagePath = mydir.getAbsolutePath() + "/" + imageFileName;
File file = new File(pictureImagePath);
Uri outputFileUri = Uri.fromFile(file);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
Fragment frag = this;
frag.getActivity();
/** Pass your fragment reference **/
frag.startActivityForResult(intent, REQUEST_IMAGE_CAPTURE); // REQUEST_IMAGE_CAPTURE = 12345

}



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
final View rootView = inflater.inflate(R.layout.camera_layout_fragment, container, false);

imageView = (ImageView) rootView.findViewById(R.id.imageView1);
return rootView;
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == 1) {
final File imgFile = new File(pictureImagePath);
if(imgFile.exists()){

getActivity().runOnUiThread(new Runnable() {
public void run() {
final Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
imageView.setImageBitmap(myBitmap);
}
});
}
}
}

public boolean isStoragePermissionGranted() {
if (Build.VERSION.SDK_INT >= 23) {
if (checkSelfPermission(this.getContext(),android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
Log.v(TAG,"Permission is granted");
return true;
} else {

Log.v(TAG,"Permission is revoked");
ActivityCompat.requestPermissions(this.getActivity(), new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
return false;
}
}
else { //permission is automatically granted on sdk<23 upon installation
Log.v(TAG,"Permission is granted");
return true;
}
}


public boolean isReadPermissionGranted() {
if (Build.VERSION.SDK_INT >= 23) {
if (checkSelfPermission(this.getContext(), Manifest.permission.READ_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
Log.v(TAG,"Permission is granted");
return true;
} else {

Log.v(TAG,"Permission is revoked");
ActivityCompat.requestPermissions(this.getActivity(), new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
return false;
}
}
else { //permission is automatically granted on sdk<23 upon installation
Log.v(TAG,"Permission is granted");
return true;
}
}
}

日志猫

    04-16 22:06:44.023 5043-5207/com.keith.draco.crecheapp I/OpenGLRenderer:               
Initialized EGL, version 1.4
04-16 22:06:44.252 5043-5043/com.keith.draco.crecheapp W/art: Before
Android 4.1, method int
android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int,
boolean) would have incorrectly overridden the package-private method in
android.widget.ListView
04-16 22:06:48.553 5043-5207/com.keith.draco.crecheapp D/OpenGLRenderer:
endAllActiveAnimators on 0xb90e4e30 (RippleDrawable) with handle
0xb90e6150
04-16 22:06:49.345 5043-5043/com.keith.draco.crecheapp W/PathParser:
Points are too far apart 4.000000596046461
04-16 22:06:49.348 5043-5043/com.keith.draco.crecheapp W/PathParser:
Points are too far apart 4.000000596046461
04-16 22:06:50.529 5043-5043/com.keith.draco.crecheapp W/PathParser:
Points are too far apart 4.000000596046461
04-16 22:06:50.531 5043-5043/com.keith.draco.crecheapp W/PathParser:
Points are too far apart 4.000000596046461
04-16 22:06:50.546 5043-5043/com.keith.draco.crecheapp V/ContentValues:
Permission is granted
04-16 22:07:05.917 5043-5043/com.keith.draco.crecheapp
I/ListPopupWindow: Could not find method setEpicenterBounds(Rect) on
PopupWindow. Oh well.
04-16 22:07:08.389 5043-5043/com.keith.draco.crecheapp I/Choreographer:
Skipped 142 frames! The application may be doing too much work on its
main thread.
04-16 22:07:10.764 5043-5043/com.keith.draco.crecheapp I/Choreographer:
Skipped 138 frames! The application may be doing too much work on its
main thread.
04-16 22:07:15.869 5043-5043/com.keith.draco.crecheapp I/Choreographer:
Skipped 135 frames! The application may be doing too much work on its
main thread.

最佳答案

many image-loading libraries available for Android ,它可以异步加载您的图像。请考虑使用一个,例如 Picasso。

如果您希望自己执行此操作,则需要安排在后台线程上执行您的 decodeFile(),然后再在主应用程序上调用 setImageBitmap() (UI) 线程。

关于java - 将图像加载到 ImageView 中会导致 "I/Choreographer: Skipped 139 frames! The application may be doing too much work on its main thread."错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43442268/

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