2017-05-18 7 views
0

Kürzlich begann ich Java und Android zu lernen, einige Android-Apps zu bauen. Auch ich bin ein wenig neu in der Codierung und die erste Anwendung Ich habe ein Problem mit hohen Daten und auch viel Speicherverbrauch. Was es tut ist: Sie geben eine genaue Adresse des Telefons ein und es zeigt alle Dateien und Subdateien im Verzeichnis an. Alles funktioniert gut, bis ich ihm eine Adresse mit vielen Dateien gebe und alles einfriert. Auch wäre ich mit jedem Feedback, Anregungen und alles andere so glücklich. Hier ist der Code:Android App Einfrieren mit Java

MainActivity.java:

package com.mycompany.MagicFiles; 

import android.app.*; 
import android.os.*; 
import android.view.*; 
import android.widget.*; 
import java.io.*; 
import java.util.*; 
import java.text.*; 

public class MainActivity extends Activity 
{ 

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
} 

public void onShowButtonClick(View view) 
{ 
    try 
    { 
     SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); 
     EditText editText = (EditText)findViewById(R.id.mainEditText1); 
     TextView textView = (TextView) findViewById(R.id.mainTextView1); 
     String dirName = editText.getText().toString(); 
     String temp = ""; 
     double bytes = 0; 

     for (File tf : listf(dirName)) 
     { 
      bytes = (tf.length()/1024); 
      temp = temp + tf.getAbsolutePath() + "\nDate: " + sdf.format(tf.lastModified()) + " Size: " + bytes + "KB\n"; 
     } 
     textView.setText(temp); 
    } 
    catch (Exception e) 
    { 
     TextView textView = (TextView) findViewById(R.id.mainTextView1); 
     textView.setText(e.toString()); 
    } 
} 

public static List<File> listf(String directoryName) 
{ 
    File directory = new File(directoryName); 

    List<File> resultList = new ArrayList<File>(); 

    // get all the files from a directory 
    File[] fList = directory.listFiles(); 
    resultList.addAll(Arrays.asList(fList)); 
    for (File file : fList) 
    { 
     if (file.isFile()) 
     { 
      System.out.println(file.getAbsolutePath()); 
     } 
     else if (file.isDirectory()) 
     { 
      resultList.addAll(listf(file.getAbsolutePath())); 
     } 
    } 
    return resultList; 
} 
} 

main.xml:

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
android:gravity="center_horizontal"> 

<TextView 
    android:layout_height="wrap_content" 
    android:text="Enter the exact location" 
    android:layout_width="wrap_content" 
    android:layout_margin="10dp" 
    android:layout_marginTop="15dp"/> 

<TextView 
    android:layout_height="wrap_content" 
    android:text="And it shows the files" 
    android:layout_width="wrap_content" 
    android:layout_margin="10dp"/> 

<EditText 
    android:layout_height="wrap_content" 
    android:ems="10" 
    android:layout_width="match_parent" 
    android:layout_margin="10dp" 
    android:id="@+id/mainEditText1"/> 

<Button 
    android:layout_height="wrap_content" 
    android:text="Show" 
    android:layout_width="match_parent" 
    android:layout_margin="10dp" 
    android:id="@+id/mainButton1" 
    android:onClick="onShowButtonClick"/> 

<HorizontalScrollView 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content"> 

    <ScrollView 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content"> 

     <TextView 
      android:layout_height="wrap_content" 
      android:text="TODO: this gonna show something up :D" 
      android:layout_width="match_parent" 
      android:id="@+id/mainTextView1"/> 

    </ScrollView> 

</HorizontalScrollView> 

AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.mycompany.MagicFiles" > 
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/the_icon" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name=".MainActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 

Wenn noch etwas (vielleicht apk ...) benötigt wird, sag es mir.

+0

Denken Sie, dass Multi-Thread helfen würde. – LiquidFlame

+0

Vielleicht listet nicht jede 'Datei' auf, sondern das' Verzeichnis'. Nach einem Klick darauf können Sie sich die Dateien –

+0

@MuratK anzeigen lassen. Nun, ich möchte Verzeichnis, Größe und zuletzt geändert Zeit, so weiß ich nicht, wie es geht: D, – LiquidFlame

Antwort

0
      public void onShowButtonClick(View view) { 
           new Thread() { 
            @Override 
            public void run() { 
             final View progressBar = findViewById(R.id.progressBar); 
             try { 

              runOnUiThread(
                new Runnable() { 
                 @Override 
                 public void run() { 
                  progressBar.setVisibility(View.VISIBLE); 
                 } 
                }); 

              SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); 
              String dirName = ((EditText) findViewById(R.id.mainEditText1)).getText().toString(); 
              String temp = ""; 
              double bytes = 0; 

              for (File tf : listf(dirName)) { 
               bytes = (tf.length()/1024); 
               temp = temp + tf.getAbsolutePath() + "\nDate: " + sdf.format(tf.lastModified()) + " Size: " + bytes + "KB\n"; 
              } 

              final String finalTemp = temp; 
              runOnUiThread(
                new Runnable() { 
                 @Override 
                 public void run() { 
                  ((TextView) findViewById(R.id.mainTextView1)).setText(finalTemp); 
                  progressBar.setVisibility(View.GONE); 
                 } 
                }); 

             } catch (final Exception e) { 
              runOnUiThread(
                new Runnable() { 
                 @Override 
                 public void run() { 
                  progressBar.setVisibility(View.GONE); 
                  ((TextView) findViewById(R.id.mainTextView1)).setText(e.toString()); 
                 } 
                }); 

             } 

            } 
           }.start(); 

} 

Und vergessen Sie nicht, einen Fortschrittsbalken zum Layout hinzufügen:

<ProgressBar 
    android:id="@+id/progressBar" 
    style="?android:attr/progressBarStyleLarge" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true" 
    android:visibility="gone"/> 
Verwandte Themen