2016-09-15 113 views
5

我試圖從Android處理草圖保存圖像,但出現錯誤。如何將我的草圖保存到用戶手機圖庫中的圖像文件?從Android處理草圖保存圖像

public void draw(){ 
     beginShape(); 
     for (int i=0; i<d.length; i++){ 
      d[i].maison(); 

      int stroke1 = (int) random(255); 
      int stroke2 = (int) random(255); 
      int stroke3 = (int) random(255); 

      stroke(stroke1, stroke2, stroke3); 
      noFill(); 
      vertex(d[i].x , d[i].y); 
     } 
     endShape(CLOSE); 

     UUID idOne = UUID.randomUUID(); 
     save(idOne + ".tga"); 

    } 

錯誤:

09-15 23:47:25.939 23317-23389/tutorials.androidstudio.fragmentsv3 E/AndroidRuntime: FATAL EXCEPTION: Animation Thread Process: tutorials.androidstudio.fragmentsv3, PID: 23317 java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File android.app.Activity.getFileStreamPath(java.lang.String)' on a null object reference at processing.core.PApplet.sketchPath(Unknown Source) at processing.core.PApplet.savePath(Unknown Source) at processing.core.PApplet.save(Unknown Source) at tutorials.androidstudio.fragmentsv3.Sketch$override.draw(Sketch.java:74) at tutorials.androidstudio.fragmentsv3.Sketch$override.access$dispatch(Sketch.java) at tutorials.androidstudio.fragmentsv3.Sketch.draw(Sketch.java:0) at processing.core.PApplet.handleDraw(Unknown Source) at processing.core.PGraphicsAndroid2D.requestDraw(Unknown Source) at processing.core.PApplet.run(Unknown Source) at java.lang.Thread.run(Thread.java:818)

回答

1

您需要指定保存目錄。通常,您的代碼的方式,Processing會將文件保存到項目目錄中的「數據」文件夾中,但在Android模式下不起作用。此外,我從來沒有真正嘗試過,但Android可能有一個類,它可以返回您的設備的某些目錄,而無需您查找它們,例如圖像,下載等。您應該看到Google Developer頁面。