Android-dev2011.02.24 18:58

에디트텍스트의 텍스트 내용을 파일로 저장하는 내용이다. 이 경우에는 인텐트를 통해서 파일경로 및 이름(Path)를 건네받도록 하였다.


@Override

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

          super.onActivityResult(requestCode, resultCode, data);

         

          switch(requestCode) {

           case ACT_SAVE:

                    if(resultCode == RESULT_OK)

                    {

                               String filePath = data.getStringExtra("file_path");

                              

                               try {

                                                     FileOutputStream fos = new FileOutputStream(filePath);

                                                     fos.write(mCustomEditText.getText().toString().getBytes());

                                                     fos.close();

                                                     showToast("File Saved");

                                          catch (FileNotFoundException e) {

                                                     // TODO Auto-generated catch block

                                                     e.printStackTrace();

                                          catch (IOException e) {

                                                     // TODO Auto-generated catch block

                                                     e.printStackTrace();

                                          }

                              

                    }



인텐트로 호출하는 액티비티에서의 내용이다. 에디트텍스트로 파일명을 입력 할 수 있도록 하였다.


                                mSaveFileNameInput = (EditText) findViewById(R.id.save_filename_input);

                                mSaveConfirmButton = (Button) findViewById(R.id.save_confirm_button);

                               

                                mSaveConfirmButton.setOnClickListener(new OnClickListener() {

                                         

                                          @Override

                                          public void onClick(View v) {

                                                     // TODO Auto-generated method stub

                                                     String fileName = mSaveFileNameInput.getText().toString();

                                                     if(fileName.endsWith(".java")|fileName.endsWith(".c")|fileName.endsWith(".cpp")|fileName.endsWith(".txt"))

                                                     {

                                                                Intent intent = new Intent();

                                                                intent.putExtra("file_path"mCurrentDirectory.getPath() + "/" + fileName);

                                                                setResult(RESULT_OK, intent);

                                                                finish();

                                                     }

                                                     else

                                                     {

                                                                showToast("Invalid File Name!");

                                                     }

                                          }

                                });


신고
Posted by 하이바쓴애
Android-dev2011.02.24 17:40

파일매니저에서 디렉토리 이동을 설명하는 예제입니다.

최상위 이동, 한단계 상위 이동, 디렉토리 진입이 가능

하도록 한 코드이지요. 기초적인 탐색기 예제라고 보시면 됩니다.


첨부파일에 풀소스코드 넣었습니다.


package com.cjhsoft.javaeditor;


import java.io.File;

import java.util.ArrayList;

import java.util.Collections;

import java.util.List;


import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.os.Environment;

import android.util.Log;

import android.view.ContextMenu;

import android.view.Gravity;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.AdapterView;

import android.widget.Button;

import android.widget.LinearLayout;

import android.widget.ListView;

import android.widget.PopupWindow;

import android.widget.Toast;

import android.widget.AdapterView.OnItemClickListener;

import android.widget.AdapterView.OnItemLongClickListener;


public class LoadFileList extends Activity {

private static final int INDEX_GO_ROOT = 0;

private static final int INDEX_UPPON_LEVEL = 1;

private static final int MENU_DELETE = 0;

private static final int MENU_CANCEL = 1;

private static final int ACT_LOADPOPUP = 0;

private static final int INDEX_DIRECTRY_START = 2;

private int INDEX_DIRECTORY_END = 2;

private int mPosition;

private List<IconfiedText> mDirectoryEntries = new ArrayList<IconfiedText>();

private List<IconfiedText> mFileEntries = new ArrayList<IconfiedText>();

private ListView mFlieListView;

private IconfiedTextListAdapter mFileListAdapter;

private File mCurrentDirectory;

@Override

public void onCreate(Bundle savedInstanceState)

{

try

{

super.onCreate(savedInstanceState);

setContentView(R.layout.load_file_list);

if(mCurrentDirectory == null)

{

 mCurrentDirectory = new File(Environment.getExternalStorageDirectory().getAbsolutePath());

}


mFlieListView = (ListView) findViewById(R.id.load_file_listview);

this.updateFileList();

mFlieListView.setOnItemLongClickListener(new OnItemLongClickListener() {


@Override

public boolean onItemLongClick(AdapterView<?> parent,

View view, int position, long id) {

// TODO Auto-generated method stub

mPosition = position;

Intent intent = new Intent(LoadFileList.this, LoadPopup.class);

startActivityForResult(intent, ACT_LOADPOPUP);


return true;

}

});

mFlieListView.setOnItemClickListener(new OnItemClickListener() {


@Override

public void onItemClick(AdapterView<?> parent, View view,

int position, long id) {

// TODO Auto-generated method stub

switch(position) {

case INDEX_GO_ROOT : 

String ext = Environment.getExternalStorageState();

if(ext.equals(Environment.MEDIA_MOUNTED)) {

mCurrentDirectory = new File("/");

}

else

{

String path = Environment.MEDIA_UNMOUNTED;

mCurrentDirectory = new File(path);

}

updateFileList();

break;

case INDEX_UPPON_LEVEL :

if(mCurrentDirectory.getParent() != null)

mCurrentDirectory = mCurrentDirectory.getParentFile();

updateFileList();

break;

default :

// 폴더이면

if(position >= INDEX_DIRECTRY_START && position <= INDEX_DIRECTORY_END)

{

String subPath = "/" + ((IconfiedText) mFileListAdapter.getItem(position)).getText();

mCurrentDirectory = new File(mCurrentDirectory.getPath() + subPath);

updateFileList();

}

else

{

String fileName = ((IconfiedText) mFileListAdapter.getItem(position)).getText();

Intent intent = new Intent();

intent.putExtra("file_path", mCurrentDirectory.getPath() + "/" + fileName);

setResult(RESULT_OK, intent);

finish();

}

break;

}

}

});

}

catch(NullPointerException e)

{

Log.v(getString(R.string.app_name), e.getMessage());

}

}

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);

switch(requestCode) {

case ACT_LOADPOPUP:

if(resultCode == RESULT_OK) {

boolean haveToDelete = data.getBooleanExtra("load_popup_result", false);

if(haveToDelete)

{

new File(mCurrentDirectory + "/" + ((IconfiedText)mFileListAdapter.getItem(mPosition)).getText()).delete();

updateFileList();

}

}

break;

}

}

public void updateFileList()

{

String ext = Environment.getExternalStorageState();

if(ext.equals(Environment.MEDIA_MOUNTED)) {

}

else

{

String path = Environment.MEDIA_UNMOUNTED;

mCurrentDirectory = new File(path);

}

mFileListAdapter = new IconfiedTextListAdapter(this);

if(mDirectoryEntries != null) {

mDirectoryEntries = new ArrayList<IconfiedText>();

}

if(mFileEntries != null) {

mFileEntries = new ArrayList<IconfiedText>();

}

mDirectoryEntries.add(new IconfiedText(".", getResources().getDrawable(R.drawable.goroot)));

mDirectoryEntries.add(new IconfiedText("..", getResources().getDrawable(R.drawable.uponelevel)));

for(File file : mCurrentDirectory.listFiles())

{

if(file.isDirectory()) mDirectoryEntries.add(new IconfiedText(file.getName(), getResources().getDrawable(R.drawable.folder)));

}

INDEX_DIRECTORY_END = mDirectoryEntries.size() - 1;

Collections.sort(mDirectoryEntries);

for(File file : mCurrentDirectory.listFiles())

{

if(file.getName().endsWith(".txt")|file.getName().endsWith(".java")|file.getName().endsWith(".c")|

file.getName().endsWith(".cpp"))

{

mFileEntries.add(new IconfiedText(file.getName(), getResources().getDrawable(R.drawable.text)));

}

}

Collections.sort(mFileEntries);

mDirectoryEntries.addAll(mFileEntries);

mFileListAdapter.setListItems(mDirectoryEntries);

mFlieListView.setAdapter(mFileListAdapter);

}

    private void showToast(String s)

    {

     Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();

    }

}

신고
Posted by 하이바쓴애
Android-dev2011.02.22 00:41

파일 로드해서 EditText에 뿌려주는 예제입니다. 파일명은 인텐트로 받아오게 했습니다.


protected void onActivityResult(int requestCode, int resultCode, Intent data) {

          super.onActivityResult(requestCode, resultCode, data);

         

          switch(requestCode) {

          case ACT_MAIN:

                    if(resultCode == RESULT_OK) {

                               String fileName = data.getStringExtra("file_name");

                              

                    String ext = Environment.getExternalStorageState();

                    String path = null;

 

                               try {

                               if(ext.equals(Environment.MEDIA_MOUNTED)) {

                                         path = mAbsolutePath + "/javaeditor/";

                               }

                               else

                               {

                                         path = Environment.MEDIA_UNMOUNTED;

                               }

                               FileInputStream fis = new FileInputStream(path + fileName);

                               byte[] fileData = new byte[fis.available()];

                               while(fis.read(fileData) != -1) {;}

                               fis.close();

                               mCustomEditText.setText(new String(fileData));

                                          catch (FileNotFoundException e) {

                                                     // TODO Auto-generated catch block

                                                     showToast("File Not Found");

                                                     e.printStackTrace();

                                          catch (IOException e) {

                                                     // TODO Auto-generated catch block

                                                     e.printStackTrace();

                                          }

                             

                    }

          }

신고
Posted by 하이바쓴애

티스토리 툴바