I would like to have an App where you can download a pdf file from the internet and search for values in it. At the first start of the App the user has to enter these values and at the second start the menue screen should be displayed. The following code let me enter the values and saves them in a file. At the second start the app should check if this file is already existing and then start the "log-in" screen or the menue. I hope i made myself clear and i hope you guys can understand what I'm trying to say^^
TestProjext.java
public class TestProjekt extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
File f = new File("Suche"); // here should be checked if the file is existing or not
if (f.exists()) {
setContentView(R.layout.menue);
} else {
setContentView(R.layout.willkommensbildschirm1);
}
}
public void buttondruck1(View view) {
setContentView(R.layout.activity_test_projekt);
}
public void buttondruck2(View view) {
setContentView(R.layout.layout2);
}
public void buttondruck3(View view) {
setContentView(R.layout.oberstufe);
}
public void buttondruck4(View view) {
Spinner Stufe = (Spinner) findViewById(R.id.spinner1);
String Klassenstufe = Stufe.getSelectedItem().toString();
Spinner Klasse = (Spinner) findViewById(R.id.spinner2);
String Buchstabe = Klasse.getSelectedItem().toString();
String Suche = Klassenstufe.concat(Buchstabe);
setContentView(R.layout.menue);
String filename = "Suche";
FileOutputStream outputStream;
try {
outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
outputStream.write(Suche.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void buttondruck5(View view) {
String Klassenstufe = "A";
EditText Abschluss = (EditText) findViewById(R.id.editText);
String Abschlussjahr = Abschluss.getText().toString();
String Suche = Klassenstufe.concat(Abschlussjahr);
setContentView(R.layout.menue);
String filename = "Suche";
FileOutputStream outputStream;
try {
outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
outputStream.write(Suche.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
new DownloadFile().execute("http://www.kgs-erfurt.de/download/vplan.pdf", "vplan.pdf"); // here the download should start
}
private class DownloadFile extends AsyncTask<String, Void, Void>{
@Override
protected Void doInBackground(String... strings) {
String fileUrl = strings[0];
String fileName = strings[1];
String extStorageDirectory = Environment.getDataDirectory().toString();
File folder = new File(extStorageDirectory, "KGS_App");
folder.mkdir();
File pdfFile = new File(folder, fileName);
try{
pdfFile.createNewFile();
}catch (IOException e){
e.printStackTrace();
}
FileDownloader.downloadFile(fileUrl, pdfFile);
return null;
}
}
}
FileDownloader.java
public class FileDownloader {
private static final int MEGABYTE = 1024 * 1024;
public static void downloadFile(String fileUrl, File directory){
try {
URL url = new URL(fileUrl);
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.connect();
InputStream inputStream = urlConnection.getInputStream();
FileOutputStream fileOutputStream = new FileOutputStream(directory);
int totalSize = urlConnection.getContentLength();
byte[] buffer = new byte[MEGABYTE];
int bufferLength = 0;
while((bufferLength = inputStream.read(buffer))>0 ){
fileOutputStream.write(buffer, 0, bufferLength);
}
fileOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
I would really appreciate your help.
TheNEXTGener4tion
0 comments:
Post a Comment