2013-03-09 62 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中将一个文件传输到另一个文件?

编辑(logcat的):

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 2013-03-09 07:51:59

+0

我的设备说不幸RecordResults已停止工作。 – Clarissa 2013-03-09 07:56:20

+0

所以请从logcat发布堆栈跟踪。只有相关的行请。 – Simon 2013-03-09 07:57:11

回答

0

你可能需要写文件的写权限。

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

另外这是a duplicated question

+0

我已经在我的清单中有。 – Clarissa 2013-03-09 08:34:33

+0

然后,也许你可以验证你的'printToFile()'是否被一个简单的调试所调用,并且你的logcat中没有引发相应的异常。 – PCoder 2013-03-09 08:39:12

+0

你不应该在网站上做[交叉发布](http://stackoverflow.com/questions/15082020/android-wont-create-a-file-or-append-to-it)。 – PCoder 2013-03-09 08:43:06

相关问题