마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

안드로이드에서 문자열을 자르고 사용하니까 앱이 중지됩니다.

0 추천
package com.cookandroid.self8_2;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.StringTokenizer;
 
 
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
 
public class Self8_2Activity extends Activity {
 
Button btnPrev, btnNext;
myPictureView myPicture;
TextView tvNumber;
int curNum;
File[] imageFiles;
String imageFname;
String fileName;
Button btnWrite;
EditText edtDiary;
String name;
byte[] name2;
int lengthName;
 
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setTitle("간단 이미지 뷰어 (변경)");
 
btnPrev = (Button) findViewById(R.id.btnPrev);
btnNext = (Button) findViewById(R.id.btnNext);
tvNumber = (TextView) findViewById(R.id.tvNumber);
myPicture = (myPictureView) findViewById(R.id.myPictureView1);
edtDiary = (EditText) findViewById(R.id.edtDiary);
btnWrite = (Button) findViewById(R.id.btnWrite);
 
imageFiles = new File("/sdcard/pictures").listFiles();
imageFname = imageFiles[0].toString();
 
System.out.println(imageFname);
myPicture.imagePath = imageFname;
tvNumber.setText("1" + "/" + imageFiles.length);
 
 
 
 
btnPrev.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (curNum <= 0) {
curNum = imageFiles.length - 1;
} else {
curNum--;
}
try{
imageFname = imageFiles[curNum].toString();
myPicture.imagePath = imageFname;
myPicture.invalidate();
tvNumber.setText((curNum + 1) + "/" + imageFiles.length);
 
name2 = imageFname.getBytes();
lengthName = name2.length-5;
name = new String(name2,0,lengthName);
 
 
fileName = name;
 
           String str = readDiary(fileName);
           edtDiary.setText(str);
           btnWrite.setEnabled(true);
}catch(Exception e){
System.out.println(e);
}
 
}
});
 
btnNext.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (curNum >= imageFiles.length - 1) {
curNum = 0;
} else {
curNum++;
}
try{
imageFname = imageFiles[curNum].toString();
 
myPicture.imagePath = imageFname;
myPicture.invalidate();
tvNumber.setText((curNum + 1) + "/" + imageFiles.length);
name2 = imageFname.getBytes();
lengthName = name2.length-5;
name = new String(name2,0,lengthName);
 
fileName = name;
 
           String str = readDiary(fileName);
           edtDiary.setText(str);
           btnWrite.setEnabled(true);
}catch(Exception e){
System.out.println(e);
 
}
}
});
try{
name2 = imageFname.getBytes();
lengthName = name2.length-5;
name = new String(name2,0,lengthName);
 
fileName = name;
String str = readDiary(fileName);
edtDiary.setText(str);
        btnWrite.setEnabled(true);
}catch(Exception e){
System.out.println(e);
 
}
 
 
 
        btnWrite.setOnClickListener(new View.OnClickListener() {
           public void onClick(View v) {
              try {
                 FileOutputStream outFs = openFileOutput(fileName,
                       Context.MODE_WORLD_WRITEABLE);
                 String str = edtDiary.getText().toString();
                 outFs.write(str.getBytes());
                 outFs.close();
                 Toast.makeText(getApplicationContext(),
                       fileName + " 이 저장됨", 0).show();
                 
  name2 = imageFname.getBytes();
  lengthName = name2.length-5;
  name = new String(name2,0,lengthName);
 
  fileName = name;
              
              } catch (Exception e) {
System.out.println(e);
 
              }
           }
        });
 
}
 
String readDiary(String fName) {
String diaryStr = null;
FileInputStream inFs;
try {
inFs = openFileInput(fName);
byte[] txt = new byte[500];
inFs.read(txt);
inFs.close();
diaryStr = (new String(txt)).trim();
btnWrite.setText("수정 하기");
} catch (IOException e) {
edtDiary.setHint("감상평 쓰세요");
btnWrite.setText("새로 저장");
}
return diaryStr;
}
 
 
}
 
 
이게 소스코드인데요 이렇게 코딩해서 이미지를 넘기면서 이미지의 이름과 같은 txt파일로 메모를 남길려고 하는데요
txt파일 이름을 이미지와 같은 이름으로 하고 싶은데 자꾸 앱이 중지되네요 실행하기도 전에...
배열인덱스번호로 txt파일을 생성하면 가능한데 이름대로 할려고 하니까 안되네요
substring을 사용해도 똑같이 앱이 중지되네요
뚜비뚜비둡 (120 포인트) 님이 2014년 10월 1일 질문
혹시 에러 코드좀 보여주실수 있나요...
콘솔창에 에러코드는 보이지 않고 로그같은 경우에는

Warning: The environment variable HOME is not set. The following directory will be used to store the Git
user global configuration and to define the default location to store repositories: 'C:\Users\Nope'. If this is
not correct please set the HOME environment variable and restart Eclipse. Otherwise Git for Windows and
EGit might behave differently since they see different configuration options.
This warning can be switched off on the Team > Git > Confirmations and Warnings preference page.
 
이거랑
 
Warning: EGit couldn't detect the installation path "gitPrefix" of native Git. Hence EGit can't respect system level
Git settings which might be configured in ${gitPrefix}/etc/gitconfig under the native Git installation directory.
The most important of these settings is core.autocrlf. Git for Windows by default sets this parameter to true in
this system level configuration. The Git installation location can be configured on the
Team > Git > Configuration preference page's 'System Settings' tab.
This warning can be switched off on the Team > Git > Confirmations and Warnings preference page.
 
이게 있네요 전 아직 공부가 부족해서 로그까지는 잘 모르거든요 ㅠㅠ

답변 달기

· 글에 소스 코드 보기 좋게 넣는 법
· 질문에 대해 추가적인 질문이나 의견이 있으면 답변이 아니라 댓글로 달아주시기 바랍니다.
표시할 이름 (옵션):
개인정보: 당신의 이메일은 이 알림을 보내는데만 사용됩니다.
스팸 차단 검사:
스팸 검사를 다시 받지 않으려면 로그인하거나 혹은 가입 하세요.
...