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.
Denken Sie, dass Multi-Thread helfen würde. – LiquidFlame
Vielleicht listet nicht jede 'Datei' auf, sondern das' Verzeichnis'. Nach einem Klick darauf können Sie sich die Dateien –
@MuratK anzeigen lassen. Nun, ich möchte Verzeichnis, Größe und zuletzt geändert Zeit, so weiß ich nicht, wie es geht: D, – LiquidFlame