I am trying load data from external storage but Getting this error and also please suggest ways by which I can load data from external storage without getting error like "too much work done by main thread" or "skipped frames"
Logcat
12/15 17:16:31: Launching 'app' on realme RMX2189.
$ adb shell am start -n "com.example.myapptoloaddata/com.example.myapptoloaddata.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -D
Waiting for application to come online: com.example.myapptoloaddata | com.example.myapptoloaddata.test
Waiting for application to come online: com.example.myapptoloaddata | com.example.myapptoloaddata.test
Connected to process 24123 on device 'realme-rmx2189-65JJ9TUKGIIZ9PUK'.
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
W/ActivityThread: Application com.example.myapptoloaddata is waiting for the debugger on port 8100...
I/System.out: Sending WAIT chunk
Connecting to com.example.myapptoloaddata
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
W/ActivityThread: Application com.example.myapptoloaddata is waiting for the debugger on port 8100...
I/System.out: Sending WAIT chunk
I/System.out: Debugger has connected
I/System.out: waiting for debugger to settle...
Connected to the target VM, address: 'localhost:8602', transport: 'socket'
I/chatty: uid=10239(com.example.myapptoloaddata) identical 2 lines
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/chatty: uid=10239(com.example.myapptoloaddata) identical 1 line
I/System.out: waiting for debugger to settle...
I/System.out: debugger has settled (1451)
E/ANR_LOG: >>> msg's executing time is too long
Blocked msg = { when=-2s854ms what=110 target=android.app.ActivityThread$H obj=AppBindData{appInfo=ApplicationInfo{a109968 com.example.myapptoloaddata}} } , cost = 2846 ms
>>>Current msg List is:
E/ANR_LOG: Current msg <1> = { when=-2s850ms what=159 target=android.app.ActivityThread$H obj=ClientTransaction hashCode, mActivityToken = android.os.BinderProxy@63f3181 }
E/ANR_LOG: Current msg <2> = { when=-2s849ms what=159 target=android.app.ActivityThread$H obj=ClientTransaction hashCode, mActivityToken = android.os.BinderProxy@63f3181 }
E/ANR_LOG: Current msg <3> = { when=-2s849ms what=9999 target=android.app.ActivityThread$H }
E/ANR_LOG: Current msg <4> = { when=-2s538ms what=149 target=android.app.ActivityThread$H obj=android.os.BinderProxy@63f3181 }
E/ANR_LOG: Current msg <5> = { when=-1s823ms what=0 target=android.app.ActivityThread$H callback=android.app.-$$Lambda$ActivityThread$A4ykhsPb8qV3ffTqpQDklHSMDJ0 }
>>>CURRENT MSG DUMP OVER<<<
W/myapptoloaddat: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed)
W/myapptoloaddat: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)
D/yes: no
I/SurfaceFactory: [static] sSurfaceFactory = com.mediatek.view.impl.SurfaceFactoryImpl@42b5e99
D/ColorViewRootUtil: initSwipState, isDisplayCompatApp false
D/ColorViewRootUtil: mScreenHeight 1600, mScreenWidth 720
D/WindowManager: Add to mViews: DecorView@6ca4b55[MainActivity], this = android.view.WindowManagerGlobal@fd4296a,pkg= com.example.myapptoloaddata
D/ViewRootImpl[MainActivity]: hardware acceleration = true , fakeHwAccelerated = false, sRendererDisabled = false, forceHwAccelerated = false, sSystemRendererDisabled = false
I/InputTransport: Create ARC handle: 0x77c869d2e0
V/PhoneWindow: DecorView setVisiblity: visibility = 0, Parent = android.view.ViewRootImpl@f3147d1, this = DecorView@6ca4b55[MainActivity]
D/TestOverScroll: getScaledOverscrollDistance: b
D/ColorViewRootUtil: initSwipState, isDisplayCompatApp false
D/ColorViewRootUtil: mScreenHeight 1600, mScreenWidth 720
D/TestOverScroll: getScaledOverscrollDistance: b
D/WindowManager: Add to mViews: DecorView@d2dcc59[MainActivity], this = android.view.WindowManagerGlobal@fd4296a,pkg= com.example.myapptoloaddata
D/ViewRootImpl[MainActivity]: hardware acceleration = true , fakeHwAccelerated = false, sRendererDisabled = false, forceHwAccelerated = false, sSystemRendererDisabled = false
I/InputTransport: Create ARC handle: 0x77c86af4c0
D/working: Working
I/Choreographer: Skipped 40 frames! The application may be doing too much work on its main thread.
E/AndroidRuntime: FATAL EXCEPTION: Asynctask #1
Process: com.example.myapptoloaddata, PID: 24123
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$4.done(AsyncTask.java:399)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
at java.util.concurrent.FutureTask.run(FutureTask.java:271)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:289)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:919)
Caused by: java.lang.NullPointerException: Attempt to get length of null array
at com.example.myapptoloaddata.MainActivity$MyclassLoader.doInBackground(MainActivity.java:90)
at com.example.myapptoloaddata.MainActivity$MyclassLoader.doInBackground(MainActivity.java:80)
at android.os.AsyncTask$3.call(AsyncTask.java:378)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:289)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:919)
I/Process: Sending signal. PID: 24123 SIG: 9
Disconnected from the target VM, address: 'localhost:8602', transport: 'socket'
java MainActivity code
public class MainActivity extends AppCompatActivity {
public static ArrayList<File> mysongs;
public static File storage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
permission();
}
mysongs=new ArrayList<>();
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
private void permission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if(ContextCompat.checkSelfPermission(MainActivity.this
, Manifest.permission.READ_EXTERNAL_STORAGE)==PackageManager.PERMISSION_GRANTED)
{
Log.d("yes","no");
Mythreadholder mythreadholder=new Mythreadholder();
mythreadholder.start();
}
else {
ActivityCompat.requestPermissions(MainActivity.this
,new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},1);
}
}
else {
ActivityCompat.requestPermissions(MainActivity.this
,new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},1);
}
}
public class MyclassLoader extends AsyncTask<Void,Void,Void>{
ProgressDialog progressDialog;
@Override
protected Void doInBackground(Void... voids) {
Log.d("working","Working");
File[] file=storage.listFiles();
assert file != null;
for(File file1:file)
{
findsongs.finder(file1);
publishProgress();
}
return null;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog=new ProgressDialog(MainActivity.this);
progressDialog.setCancelable(true);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMax(300);
progressDialog.setMessage("Data is being loaded");
progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "cancel"
, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
progressDialog.show();
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
if(mysongs!=null)
{
progressDialog.setProgress(mysongs.size());
}
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
progressDialog.dismiss();
}
}
public class Mythreadholder extends Thread{
@Override
public void run() {
super.run();
runOnUiThread(new Runnable() {
@Override
public void run() {
storage=Environment.getExternalStorageDirectory();
MyclassLoader myclassLoader=new MyclassLoader();
myclassLoader.execute();
}
});
}
}
Method code
public class findsongs {
public static void finder(File file)
{
File[] filli=file.listFiles();
for(File file1:filli)
{
if(file1.isDirectory() && !file1.isHidden())
{
finder(file1);
}
else {
if(file1.getName().endsWith(".mp3"))
{
MainActivity.mysongs.add(file1);
}
}
}
}
}
Please suggest me some methods or ways by which I can load data efficiently. I am new to this so please suggest how can I resolve these and how should I load data without error. Please suggest how I should load data.