2015-09-07 59 views
0

我試圖將文件保存在「Parse.com」在我的Android application.However,我正在以下錯誤無法將數據保存到Parse.com雲服務

java.lang.IllegalArgumentException: invalid type for value: class java.io.File 

下面提到纏着是我的代碼:

protected void onCreate(Bundle savedInstanceState) { 

    try { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main_activity); 
     Parse.enableLocalDatastore(this); 
     Parse.initialize(this, "nNDrIDPB8evzutqXnMkLNwV5l0yKm6RHQDshJhLN", "bkIb6fI9RWgezwulCHismlNUeYBz9sNxFaLxf8NB"); 
     File f = new File("C:\\Users\\rishii\\AndroidStudioProjects\\Introduction\\file_input9.txt"); 
     FileInputStream fileInputStream = new FileInputStream(f); 
     byte fileContent[] = new byte[(int)f.length()]; 
     fileInputStream.read(fileContent); 
     ParseFile parseFile = new ParseFile(fileContent); 
     ParseObject dataObject = new ParseObject("file_content!"); 
     dataObject.put("file",parseFile); 
     dataObject.saveInBackground(); 
     ParseUser.enableAutomaticUser(); 
     ParseACL defaultACL = new ParseACL(); 
     ParseACL.setDefaultACL(defaultACL, true); 


    } 
    catch (Exception e) { 
     e.printStackTrace(); 
    } 

我敢肯定,我已經給是correct.Below提到的文件路徑是我的堆棧跟蹤:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.rishii.introduction/com.example.rishii.introduction.Main_activity}: java.lang.IllegalArgumentException: invalid type for value: class java.io.File 
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
     at android.app.ActivityThread.access$800(ActivityThread.java:135) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
     at android.os.Handler.dispatchMessage(Handler.java:102) 
     at android.os.Looper.loop(Looper.java:136) 
     at android.app.ActivityThread.main(ActivityThread.java:5021) 
     at java.lang.reflect.Method.invokeNative(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:515) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643) 
     at dalvik.system.NativeStart.main(Native Method) 
Caused by: java.lang.IllegalArgumentException: invalid type for value: class java.io.File 
     at com.parse.ParseObject.performPut(ParseObject.java:3152) 
     at com.parse.ParseObject.put(ParseObject.java:3139) 
     at com.example.rishii.introduction.Main_activity.onCreate(Main_activity.java:77) 
     at android.app.Activity.performCreate(Activity.java:5231) 
     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1090) 
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 
     at 
    android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 

任何建議是有幫助的!

+1

作爲未來問題的建議:您不應在Stack Overflow上發佈您的Parse訪問令牌。 – fixmycode

+0

@fixmycode謝謝!!會記住這一點 –

回答

1

在android中,您不能直接從本地系統導入文件。

你要先將它保存到sdacrd中或將該文本文件複製到你的android項目的原始文件夾中。

然後,您可以將它保存到任何適合apis的地方。

File f = new File("C:\\Users\\rishii\\AndroidStudioProjects\\Introduction\\file_input9.txt 

此行正在引發問題。