2013-03-09 2 views
-1
public class MainActivity extends Activity implements OnTouchListener { 
private float x; 
private float y; 
Rect clearButton = new Rect(300,570,400,620); 
Rect resultsButton = new Rect(900,570,1000,620); 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    MyCustomPanel view = new MyCustomPanel(this); 

    ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT, 
    LayoutParams.MATCH_PARENT); 
    addContentView(view, params); 
    view.setOnTouchListener(this); 

} 
private class MyCustomPanel extends View { 

    public MyCustomPanel(Context context) { 
     super(context); 

    }@Override 
    public void draw(Canvas canvas) { 

     Paint paint = new Paint(); 
     paint.setColor(Color.RED); 
     canvas.drawCircle(x, y, 5, paint); 
     canvas.drawRect(clearButton, paint); 
     canvas.drawRect(resultsButton, paint); 
    } 
} 
public boolean onTouch(View v, MotionEvent event) { 
    x = event.getX(); 
    y = event.getY(); 
    if (clearButton.contains((int)x,(int) y)){ 
     deleteFile("recordResults.txt"); 
    } 
    else if (resultsButton.contains((int) x, (int) y)) { 
      try{ 
       FileInputStream fis = openFileInput("recordResults.txt"); 

       InputStreamReader isr = new InputStreamReader(fis); 
       BufferedReader br = new BufferedReader(isr); 
       StringBuilder results = new StringBuilder(); 
       String resultsToFile; 
       String line; 
       while((line = br.readLine()) != null){ 
        results.append(line); 
       } 
       resultsToFile = results.toString(); 
       String root = Environment.getExternalStorageDirectory().getAbsolutePath() + "/results.txt"; 
       FileOutputStream fos = openFileOutput(root, MODE_APPEND); 
       OutputStreamWriter osw = new OutputStreamWriter(fos); 
       osw.write(resultsToFile); 
       osw.flush(); 
       osw.close(); 
      } 
      catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
    } 
    else{ 

     try { 
      printToFile(x, y); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
    v.invalidate(); 
    return true; 
} 
public void printToFile(Float x, Float y) throws IOException { 

    final String record = new String("x:" + String.valueOf(x) + ", y:" + String.valueOf(y)); 
    FileOutputStream fos = openFileOutput("recordResults.txt", MODE_APPEND); 
    OutputStreamWriter osw = new OutputStreamWriter(fos); 
    osw.write(record); 
    osw.flush(); 
    osw.close(); 

} 

} 

이 코드가 있으며 만든 파일을 다른 위치로 복사하려고합니다. resultsButton Rect를 누를 때마다 작업이 중단됩니다. 왜 이런거야? 이 문제를 해결할 더 좋은 방법이 있습니까? 감사!Android에서 파일을 다른 파일로 전송 하시겠습니까?

편집 (로그 캣) :

03-09 08:07:44.139: W/System.err(841): java.io.FileNotFoundException: /data/data/com.example.recordresults/files/recordResults.txt: open failed: ENOENT (No such file or directory) 
03-09 08:07:44.239: W/System.err(841): at com.example.recordresults.MainActivity.onTouch(MainActivity.java:74) 
03-09 08:07:44.508: W/System.err(841): java.io.FileNotFoundException: /data/data/com.example.recordresults/files/recordResults.txt: open failed: ENOENT (No such file or directory) 
03-09 08:07:44.553: W/System.err(841): at com.example.recordresults.MainActivity.onTouch(MainActivity.java:74) 
03-09 08:07:54.369: E/MessageQueue-JNI(841): at com.example.recordresults.MainActivity.onTouch(MainActivity.java:86) 
03-09 08:07:54.429: E/AndroidRuntime(841): at com.example.recordresults.MainActivity.onTouch(MainActivity.java:86) 
+0

'작동이 멈 춥니 다. ' 무슨 소리 야? 정확히 어떻게됩니까? – Simon

+0

장치에 불행히도 RecordResults가 작동하지 않는다고 표시됩니다. – Clarissa

+0

그래서 logcat에서 스택 추적을 게시하십시오. 관련 전화 만주십시오. – Simon

답변

0

당신은 아마 파일을 작성하기위한 쓰기 권한이 필요합니다.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

또한 a duplicated question입니다.

+0

이미 내 매니페스트에서 그걸 가지고 있습니다. – Clarissa

+0

그러면,'printToFile()'이 간단한 디버그에 의해 호출되고, logcat에 상응하는 예외가 발생하지 않았는지 확인할 수 있습니다. – PCoder

+0

그리고 사이트에서 [교차 게시] (http://stackoverflow.com/questions/15082020/android-wont-create-a-file-or-append-to-it)를 수행하지 않아야합니다. – PCoder

관련 문제