我正在尝试加载数据,但无法解决我面临的问题

lsmepo6l  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(417)

我正在尝试从外部存储加载所有PDF。问题是,加载pdf的代码在我只加载pdf的不同项目中运行良好,但在有多个特性的项目中,它失败了,因此我无法解决问题所在。

public class MainActivity extends AppCompatActivity {

    FloatingActionButton fab;

    DrawerLayout drawer;
    Handler handler=new Handler();
    RecyclerView recyclerView;
    MycustomAdapterForPdf mycustomAdapterForPdf;
    public static ArrayList<String> pdfname;
    public static ArrayList<String> pdfpath;

    @SuppressLint("RestrictedApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            peeMission();
        }

        ActionBar actionBar=getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setHomeAsUpIndicator(R.drawable.ic_menu);

        drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView = findViewById(R.id.nav_view);

        //Here i have created function for event occuring when any
        //any of the navigation item is selected
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem item) {
                int id=item.getItemId();

                switch (id){
                    case R.id.Music:
                            Intent intent=new Intent(MainActivity.this,Musiclist.class);
                            startActivity(intent);
                        break;

                    default:
                        Toast.makeText(MainActivity.this
                                ,"working ",Toast.LENGTH_LONG).show();

                }

                return false;
            }
        });
        recyclerView=findViewById(R.id.pdfrecycleview);
        //for getting pdf here i am setting for home page layout in different thread
        pdfname=new ArrayList<>();
        pdfpath=new ArrayList<>();
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.Q)
        {

            Thread thread=new Thread(){

                @Override
                public void run() {

                getpdfname(MainActivity.this);

                }
            };

            thread.start();

        }

        Thread thread1=new Thread(){

            @Override
            public void run() {
                super.run();

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                mycustomAdapterForPdf=new MycustomAdapterForPdf(MainActivity.this);
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        recyclerView.setAdapter(mycustomAdapterForPdf);
                        recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
                    }
                },100);

            }
        };
        thread1.start();

        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.

        //here i initialized floating button
        fab = findViewById(R.id.fab);
        //here i am defining the onclick action to be performed by floating button
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent=new Intent(MainActivity.this,Togetbook.class);
                startActivity(intent);

            }
        });

    }

    public void getpdfname(Context context){

        ContentResolver cr=context.getContentResolver();
        Uri uri=MediaStore.Files.getContentUri("external");
        String[] projection={MediaStore.Files.FileColumns.TITLE,MediaStore.Files.FileColumns.DATA};
        String selection=MediaStore.Files.FileColumns.MIME_TYPE+"=?";
        String mimetype=MimeTypeMap.getSingleton().getMimeTypeFromExtension("pdf");
        String[] pdfarg=new String[]{mimetype};

        Cursor c=cr.query(uri,projection,selection,pdfarg,null);

        if(c!=null)
        {
            while (c.moveToNext())
            {
                String name=c.getString(0);
                String path=c.getString(1);
                pdfname.add(name);
                pdfpath.add(path);
                Log.d("name",path);

            }

        }

        c.close();
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    //this is for option menu item being selected, here is the action to performed
    //based on item being selected
    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {

        int id=item.getItemId();
        switch (id){

            case R.id.brightness :
                Intent intent=new Intent(MainActivity.this,Brightnesssetter.class);
                startActivity(intent);
                break;
            case R.id.NightMode:
                    setBrightness();

                break;
                //the next case written below is to start navigation bar when hamburger is clicked
            case android.R.id.home:
                drawer.openDrawer(GravityCompat.START );
                return true;

        }

        return super.onOptionsItemSelected(item);

    }

    //brightness ke liye NightMode ke liye
    @RequiresApi(api = Build.VERSION_CODES.M)
    public void setBrightness(){
        boolean check= Settings.System.canWrite(MainActivity.this);
        if(check)
        {
            int val= (int) (5*2.5);
            Settings.System.putInt(getContentResolver()
                    ,Settings.System.SCREEN_BRIGHTNESS_MODE
                    ,Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
            Settings.System.putInt(getContentResolver()
                    ,Settings.System.SCREEN_BRIGHTNESS,val);

        }
        else {
            permission();
        }

    }
        //this is also for brightness bar specifically NightMode
    @RequiresApi(api = Build.VERSION_CODES.M)
    private void permission() {
        Context context=getApplicationContext();
        Intent intent=new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);

    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    public void peeMission()
    {
        if(checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)== PackageManager.PERMISSION_GRANTED)
        {
            Log.d("permission","granted");

        }
        else
        {
            ActivityCompat.requestPermissions(MainActivity.this
                    ,new String[] {Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE},2);
        }

    }

}

我的日志显示

2020-12-22 23:24:40.800 22110-22110/com.example.read_it1 W/xample.read_it: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed)
2020-12-22 23:24:40.803 22110-22110/com.example.read_it1 W/xample.read_it: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)
2020-12-22 23:24:41.341 22110-22110/com.example.read_it1 D/permission: granted
2020-12-22 23:24:41.371 22110-22110/com.example.read_it1 I/SurfaceFactory: [static] sSurfaceFactory = com.mediatek.view.impl.SurfaceFactoryImpl@c149856
2020-12-22 23:24:41.414 22110-22110/com.example.read_it1 D/ColorViewRootUtil: initSwipState, isDisplayCompatApp false
2020-12-22 23:24:41.417 22110-22110/com.example.read_it1 D/ColorViewRootUtil: mScreenHeight 1600, mScreenWidth 720
2020-12-22 23:24:41.422 22110-22110/com.example.read_it1 D/WindowManager: Add to mViews: DecorView@159e3e2[MainActivity], this = android.view.WindowManagerGlobal@9811373,pkg= com.example.read_it1
2020-12-22 23:24:41.441 22110-22110/com.example.read_it1 D/ViewRootImpl[MainActivity]: hardware acceleration = true , fakeHwAccelerated = false, sRendererDisabled = false, forceHwAccelerated = false, sSystemRendererDisabled = false
2020-12-22 23:24:41.458 22110-22110/com.example.read_it1 I/InputTransport: Create ARC handle: 0x7b9fe0c5c0
2020-12-22 23:24:41.460 22110-22110/com.example.read_it1 V/PhoneWindow: DecorView setVisiblity: visibility = 0, Parent = android.view.ViewRootImpl@8b65c2e, this = DecorView@159e3e2[MainActivity]
2020-12-22 23:24:41.472 22110-22110/com.example.read_it1 E/ANR_LOG: >>> msg's executing time is too long
2020-12-22 23:24:41.472 22110-22110/com.example.read_it1 E/ANR_LOG: Blocked msg = { when=-2s273ms what=159 target=android.app.ActivityThread$H obj=ClientTransaction hashCode, mActivityToken = android.os.BinderProxy@909b9b9 } , cost  = 1735 ms
2020-12-22 23:24:41.472 22110-22110/com.example.read_it1 E/ANR_LOG: >>>Current msg List is:
2020-12-22 23:24:41.472 22110-22110/com.example.read_it1 E/ANR_LOG: Current msg <1> = { when=-2s272ms what=159 target=android.app.ActivityThread$H obj=ClientTransaction hashCode, mActivityToken = android.os.BinderProxy@909b9b9 }
2020-12-22 23:24:41.473 22110-22110/com.example.read_it1 E/ANR_LOG: Current msg <2> = { when=-2s271ms what=9999 target=android.app.ActivityThread$H }
2020-12-22 23:24:41.473 22110-22110/com.example.read_it1 E/ANR_LOG: Current msg <3> = { when=-1s321ms what=149 target=android.app.ActivityThread$H obj=android.os.BinderProxy@909b9b9 }
2020-12-22 23:24:41.473 22110-22110/com.example.read_it1 E/ANR_LOG: Current msg <4> = { when=-1s197ms what=0 target=android.app.ActivityThread$H callback=android.app.-$$Lambda$ActivityThread$A4ykhsPb8qV3ffTqpQDklHSMDJ0 }
2020-12-22 23:24:41.474 22110-22110/com.example.read_it1 E/ANR_LOG: Current msg <5> = { when=-183ms what=0 target=android.os.Handler callback=androidx.core.content.res.ResourcesCompat$FontCallback$2 }
2020-12-22 23:24:41.474 22110-22110/com.example.read_it1 E/ANR_LOG: Current msg <6> = { when=-174ms what=0 target=android.os.Handler callback=androidx.core.content.res.ResourcesCompat$FontCallback$2 }
2020-12-22 23:24:41.474 22110-22110/com.example.read_it1 E/ANR_LOG: Current msg <7> = { when=-136ms what=0 target=android.os.Handler callback=androidx.core.content.res.ResourcesCompat$FontCallback$2 }
2020-12-22 23:24:41.474 22110-22110/com.example.read_it1 E/ANR_LOG: Current msg <8> = { when=-133ms what=0 target=android.os.Handler callback=androidx.core.content.res.ResourcesCompat$FontCallback$2 }
2020-12-22 23:24:41.475 22110-22110/com.example.read_it1 E/ANR_LOG: Current msg <9> = { when=-58ms what=0 target=android.view.ViewRootImpl$ViewRootHandler callback=android.view.ViewRootImpl$4 }
2020-12-22 23:24:41.475 22110-22110/com.example.read_it1 E/ANR_LOG: Current msg <10> = { when=-29ms barrier=0 }
2020-12-22 23:24:41.475 22110-22110/com.example.read_it1 E/ANR_LOG: >>>CURRENT MSG DUMP OVER<<<
2020-12-22 23:24:41.494 22110-22110/com.example.read_it1 I/Choreographer: Skipped 2 frames!  The application may be doing too much work on its main thread.
2020-12-22 23:24:41.613 22110-22243/com.example.read_it1 E/GED: Failed to get GED Log Buf, err(0)
2020-12-22 23:24:41.617 22110-22243/com.example.read_it1 E/EGL-gift: Cannot load register apis
2020-12-22 23:24:41.626 22110-22110/com.example.read_it1 E/RecyclerView: No adapter attached; skipping layout
2020-12-22 23:24:41.662 22110-22243/com.example.read_it1 D/Surface: Surface::connect(this=0x7bf575d000,api=1)
2020-12-22 23:24:41.664 22110-22243/com.example.read_it1 D/Surface: Surface::setBufferCount(this=0x7bf575d000,bufferCount=3)
2020-12-22 23:24:41.665 22110-22243/com.example.read_it1 D/Surface: Surface::allocateBuffers(this=0x7bf575d000)
2020-12-22 23:24:41.800 22110-22243/com.example.read_it1 W/Gralloc3: mapper 3.x is not supported
2020-12-22 23:24:41.811 22110-22243/com.example.read_it1 E/ion: ioctl c0044901 failed with code -1: Invalid argument
2020-12-22 23:24:42.008 22110-22110/com.example.read_it1 I/Choreographer: Skipped 29 frames!  The application may be doing too much work on its main thread.
2020-12-22 23:24:42.081 22110-22110/com.example.read_it1 I/Choreographer: Skipped 4 frames!  The application may be doing too much work on its main thread.
2020-12-22 23:24:42.115 22110-22110/com.example.read_it1 D/ColorViewRootUtil: initSwipState, isDisplayCompatApp false
2020-12-22 23:24:42.119 22110-22110/com.example.read_it1 D/ColorViewRootUtil: mScreenHeight 1600, mScreenWidth 720
2020-12-22 23:24:42.125 22110-22110/com.example.read_it1 I/Choreographer: Skipped 1 frames!  The application may be doing too much work on its main thread.
2020-12-22 23:24:44.542 22110-22193/com.example.read_it1 I/xample.read_it: ProcessProfilingInfo new_methods=1739 is saved saved_to_disk=1 resolve_classes_delay=5000
2020-12-22 23:24:51.326 22110-22110/com.example.read_it1 D/ColorViewRootUtil: nav bar mode ignore false downX 284 downY 940 mScreenHeight 1600 mScreenWidth 720 mStatusBarHeight 36 globalScale 1.125 nav mode 0 rotation 0 event MotionEvent { action=ACTION_DOWN, actionButton=0, id[0]=0, x[0]=284.0, y[0]=940.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=123710466, downTime=123710466, deviceId=1, source=0x1002, displayId=0 }
2020-12-22 23:24:51.651 22110-22110/com.example.read_it1 I/Choreographer: Skipped 5 frames!  The application may be doing too much work on its main thread.
2020-12-22 23:25:07.487 22110-22110/com.example.read_it1 I/Choreographer: Skipped 2 frames!  The application may be doing too much work on its main thread.
2020-12-22 23:25:07.489 22110-22243/com.example.read_it1 I/GED: ged_boost_gpu_freq, level 100, origin 2, final_idx 2, oppidx_max 2, oppidx_min 0
2020-12-22 23:25:12.129 22110-22243/com.example.read_it1 D/Surface: Surface::disconnect(this=0x7bf575d000,api=1)
2020-12-22 23:25:12.173 22110-22110/com.example.read_it1 V/PhoneWindow: DecorView setVisiblity: visibility = 4, Parent = android.view.ViewRootImpl@8b65c2e, this = DecorView@159e3e2[MainActivity]
2020-12-22 23:25:42.424 22820-22820/com.example.read_it1 W/xample.read_it: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed)
2020-12-22 23:25:42.429 22820-22820/com.example.read_it1 W/xample.read_it: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)
2020-12-22 23:25:43.250 22820-22820/com.example.read_it1 D/permission: granted
2020-12-22 23:25:43.318 22820-22820/com.example.read_it1 I/SurfaceFactory: [static] sSurfaceFactory = com.mediatek.view.impl.SurfaceFactoryImpl@37104ad
2020-12-22 23:25:43.375 22820-22820/com.example.read_it1 D/ColorViewRootUtil: initSwipState, isDisplayCompatApp false
2020-12-22 23:25:43.377 22820-22820/com.example.read_it1 D/ColorViewRootUtil: mScreenHeight 1600, mScreenWidth 720
2020-12-22 23:25:43.391 22820-22820/com.example.read_it1 D/WindowManager: Add to mViews: DecorView@4463ea9[MainActivity], this = android.view.WindowManagerGlobal@8b65c2e,pkg= com.example.read_it1
2020-12-22 23:25:43.439 22820-22820/com.example.read_it1 D/ViewRootImpl[MainActivity]: hardware acceleration = true , fakeHwAccelerated = false, sRendererDisabled = false, forceHwAccelerated = false, sSystemRendererDisabled = false
2020-12-22 23:25:43.487 22820-22820/com.example.read_it1 I/InputTransport: Create ARC handle: 0x7c8d668c40
2020-12-22 23:25:43.491 22820-22820/com.example.read_it1 V/PhoneWindow: DecorView setVisiblity: visibility = 0, Parent = android.view.ViewRootImpl@e1b1c65, this = DecorView@4463ea9[MainActivity]
2020-12-22 23:25:43.611 22820-22820/com.example.read_it1 E/ANR_LOG: >>> msg's executing time is too long
2020-12-22 23:25:43.611 22820-22820/com.example.read_it1 E/ANR_LOG: Blocked msg = { when=-4s227ms what=159 target=android.app.ActivityThread$H obj=ClientTransaction hashCode, mActivityToken = android.os.BinderProxy@dd0a1fe } , cost  = 2770 ms
2020-12-22 23:25:43.611 22820-22820/com.example.read_it1 E/ANR_LOG: >>>Current msg List is:
2020-12-22 23:25:43.612 22820-22820/com.example.read_it1 E/ANR_LOG: Current msg <1> = { when=-4s216ms what=159 target=android.app.ActivityThread$H obj=ClientTransaction hashCode, mActivityToken = android.os.BinderProxy@dd0a1fe }
2020-12-22 23:25:43.613 22820-22820/com.example.read_it1 E/ANR_LOG: Current msg <2> = { when=-4s206ms what=159 target=android.app.ActivityThread$H obj=ClientTransaction hashCode, mActivityToken = android.os.BinderProxy@dd0a1fe }
2020-12-22 23:25:43.614 22820-22820/com.example.read_it1 E/ANR_LOG: Current msg <3> = { when=-4s204ms what=159 target=android.app.ActivityThread$H obj=ClientTransaction hashCode, mActivityToken = android.os.BinderProxy@dd0a1fe }
2020-12-22 23:25:43.616 22820-22820/com.example.read_it1 E/ANR_LOG: Current msg <4> = { when=-4s200ms what=9999 target=android.app.ActivityThread$H }
2020-12-22 23:25:43.619 22820-22820/com.example.read_it1 E/ANR_LOG: Current msg <5> = { when=-4s94ms what=149 target=android.app.ActivityThread$H obj=android.os.BinderProxy@dd0a1fe }
2020-12-22 23:25:43.621 22820-22820/com.example.read_it1 E/ANR_LOG: Current msg <6> = { when=-4s77ms what=0 target=android.app.ActivityThread$H callback=com.android.internal.util.function.pooled.PooledLambdaImpl }
2020-12-22 23:25:43.621 22820-22820/com.example.read_it1 E/ANR_LOG: Current msg <7> = { when=-3s686ms what=159 target=android.app.ActivityThread$H obj=ClientTransaction hashCode, mActivityToken = android.os.BinderProxy@dd0a1fe }
2020-12-22 23:25:43.622 22820-22820/com.example.read_it1 E/ANR_LOG: Current msg <8> = { when=-3s685ms what=137 target=android.app.ActivityThread$H arg1=1 obj=android.os.BinderProxy@dd0a1fe }
2020-12-22 23:25:43.623 22820-22820/com.example.read_it1 E/ANR_LOG: Current msg <9> = { when=-444ms what=0 target=android.os.Handler callback=androidx.core.content.res.ResourcesCompat$FontCallback$2 }
2020-12-22 23:25:43.623 22820-22820/com.example.read_it1 E/ANR_LOG: Current msg <10> = { when=-430ms what=0 target=android.os.Handler callback=androidx.core.content.res.ResourcesCompat$FontCallback$2 }
2020-12-22 23:25:43.623 22820-22820/com.example.read_it1 E/ANR_LOG: >>>CURRENT MSG DUMP OVER<<<
2020-12-22 23:25:43.709 22820-22820/com.example.read_it1 V/PhoneWindow: DecorView setVisiblity: visibility = 4, Parent = android.view.ViewRootImpl@e1b1c65, this = DecorView@4463ea9[MainActivity]
2020-12-22 23:25:43.725 22820-22820/com.example.read_it1 I/Choreographer: Skipped 16 frames!  The application may be doing too much work on its main thread.
2020-12-22 23:25:44.066 22820-22820/com.example.read_it1 I/Choreographer: Skipped 18 frames!  The application may be doing too much work on its main thread.
2020-12-22 23:25:44.981 22820-22942/com.example.read_it1 I/xample.read_it: ProcessProfilingInfo new_methods=1243 is saved saved_to_disk=1 resolve_classes_delay=5000

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题