AssetActivity.java
Dosyayı İndir
package com.godoro.androiddevices;
import android.app.Activity;
import android.content.res.*;
import android.graphics.drawable.*;
import android.os.Bundle;
import android.util.*;
import android.view.*;
import android.widget.*;
import org.w3c.dom.*;
import java.io.*;
import javax.xml.parsers.*;
public class AssetActivity extends Activity {
private String XML_FILE_NAME ="exam.xml";
private String XML_FILE_PATH ="exams/"+ XML_FILE_NAME;
private TextView contentView;
private String IMAGE_FILE_PATH="images/godoro_logo.jpg";
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_asset);
contentView=(TextView) findViewById(R.id.contentView);
imageView=(ImageView) findViewById(R.id.imageView);
//contentView.setText(getText(R.string.resourceString));
contentView.setText(getString(R.string.parametricString,3));
}
public void onLoadXmlClick(View view) {
try {
AssetManager assetManager = getAssets();
InputStream in=assetManager.open(XML_FILE_PATH);
loadXml(in);
}catch(Exception e){
Log.e("GodoroAndroid","Varlık yükleme yanlışlığı ",e);
String message="Varlık yükleme yanlışlığı "+e.getMessage();
Toast.makeText(AssetActivity.this,message,Toast.LENGTH_LONG).show();
}
}
private void loadXml(InputStream in) throws Exception{
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
Document document=builder.parse(in);
showDocument(document);
in.close();
}
private void showDocument(Document document){
Element exam=document.getDocumentElement();
String id=exam.getAttribute("id");
String title=exam.getElementsByTagName("title").item(0).getTextContent();
contentView.setText("Sınav "+id+": "+title+"\r\n");
NodeList questionList=exam.getElementsByTagName("question");
for (int i = 0; i < questionList.getLength(); i++) {
Element question=(Element) questionList.item(i);
int number=Integer.parseInt(question.getAttribute("number"));
String body=question.getElementsByTagName("body")
.item(0).getTextContent();
contentView.setText(contentView.getText()
+"\t"+number+". "+body+"\r\n");
NodeList optionList=question.getElementsByTagName("option");
for (int j = 0; j < optionList.getLength(); j++) {
Element option = (Element) optionList.item(j);
String code=option.getAttribute("code");
String text=option.getTextContent();
contentView.setText(contentView.getText()
+"\t\t"+code+") "+text+"\r\n");
}
}
}
public void onCopyXmlClick(View view) {
try {
AssetManager assetManager = getAssets();
File directory=getFilesDir();
//File directory=Environment.getExternalStorageDirectory();
//File directory=Environment.getExternalStoragePublicDirectory(
// Environment.DIRECTORY_DOCUMENTS);
InputStream in=assetManager.open(XML_FILE_PATH);
File file=new File(directory, XML_FILE_NAME);
FileOutputStream out=new FileOutputStream(file);
copyFile(in,out);
contentView.setText("Dosya göçürüldü "+file.getAbsolutePath());
}catch(Exception e){
Log.e("GodoroAndroid","Varlık göçürme yanlışlığı ",e);
String message="Varlık göçürme yanlışlığı"+e.getMessage();
Toast.makeText(AssetActivity.this,message,Toast.LENGTH_LONG).show();
}
}
private void copyFile(InputStream in,OutputStream out)throws Exception{
byte[] buffer=new byte[1024];
int length;
while((length=in.read(buffer))!= -1){
out.write(buffer,0,length);
}
in.close();
out.close();
}
public void onLoadImageClick(View view) {
try {
AssetManager assetManager = getAssets();
InputStream in=assetManager.open(IMAGE_FILE_PATH);
Drawable drawable = Drawable.createFromStream(in, null);
imageView.setImageDrawable(drawable);
contentView.setText("Görüntü gösterildi ");
}catch(Exception e){
Log.e("GodoroAndroid","Varlık yükleme yanlışlığı ",e);
String message="Varlık yükleme yanlışlığı "+e.getMessage();
Toast.makeText(AssetActivity.this,message,Toast.LENGTH_LONG).show();
}
}
}
Dosyayı İndir