請別人幫我!我不知道爲什麼文件不能製作
我做了一些代碼,代碼是TCP Sock_program。
並且此代碼不起作用!
我覺得這個問題發生是因爲文件沒有創建。
這是我的代碼。 (我的手機是安卓2.3.6)
public class DownActivity extends Activity {
public static int contentNum;
public static String body=new String();
public static String fName=new String();
public static int fSize;
public static synchronized String getFilePath(String userID, String fName)
{
String sdcard = Environment.getExternalStorageState();
File file = null;
if (!sdcard.equals(Environment.MEDIA_MOUNTED))
{
// case: SDcard doesn't exist
file = Environment.getRootDirectory();
}
else
{
// case: SDcard does exist
file = Environment.getExternalStorageDirectory();
}
String dir = file.getAbsolutePath() + String.format("/tcp_cloud/%s",userID);
String path = file.getAbsolutePath() + String.format("/tcp_cloud/%s/%s\n",userID,fName);
file = new File(dir);
if (!file.exists())
{
// dir create
file.mkdirs();
}
// return File Path
return path;
}
public void connect()
{
try{
Socket socket=new Socket(MainActivity.servIP, MainActivity.servPort);
DataOutputStream dos;
DataInputStream dis;
dis=new DataInputStream(socket.getInputStream());
dos=new DataOutputStream(socket.getOutputStream());
byte[]flag=new byte[3];
byte[]num=new byte[1];
byte[]uID=new byte[17];
String path=new String();
path=getFilePath(LoginActivity.usrName,fName);
**File f=new File(path);**
flag="05".getBytes("EUC_KR");
num=Integer.toHexString(contentNum).getBytes("EUC_KR");
uID=LoginActivity.usrName.getBytes("EUC_KR");
dos.write(flag);
dos.flush();
dos.write(num);
dos.flush();
dos.write(uID);
dos.flush();
**try{
f.createNewFile();
}catch(IOException e){
e.printStackTrace();
}**
f.setWritable(true,false);
f.setReadable(true, false);
**FileOutputStream fos=new FileOutputStream(f);**
Toast.makeText(getApplicationContext(),f.getAbsolutePath(),Toast.LENGTH_LONG).show();
BufferedOutputStream bos=new BufferedOutputStream(fos);
dos=new DataOutputStream(bos);
int len;
int size = 512;
byte[] data = new byte[size];
while ((len = dis.read(data,0,size))!=-1)
{
dos.write(data);
}
dos.flush();
Toast.makeText(getApplicationContext(),path+" saved",Toast.LENGTH_LONG).show();
dos.close();
bos.close();
fos.close();
dos.close();
dis.close();
socket.close();
}catch (Exception e){
e.printStackTrace();
}
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_down);
String title=new String();
if(contentNum==0)
title=LoginActivity.title1;
else if(contentNum==1)
title=LoginActivity.title2;
else if(contentNum==2)
title=LoginActivity.title3;
else if(contentNum==3)
title=LoginActivity.title4;
else if(contentNum==4)
title=LoginActivity.title5;
TextView bodytit=(TextView) findViewById(R.id.bodyTitle);
TextView bodydat=(TextView) findViewById(R.id.bodyBody);
Button filedat=(Button) findViewById(R.id.filename);
bodytit.setText(title);
bodydat.setText(body);
filedat.setText(fName+"(크기: "+fSize+")");
filedat.setOnClickListener(new OnClickListener(){
public void onClick(View v){
connect();
}
});
}
}
,這是錯誤日誌
12-16 12:27:24.414: D/dalvikvm(5797): GC_EXTERNAL_ALLOC freed 52K, 49% free 2772K/5379K, external 1596K/2108K, paused 24ms
12-16 12:27:36.434: D/dalvikvm(5797): GC_EXTERNAL_ALLOC freed 69K, 47% free 2856K/5379K, external 5192K/6444K, paused 20ms
12-16 12:27:46.426: W/System.err(5797): java.io.IOException: Invalid argument
12-16 12:27:46.434: W/System.err(5797): at java.io.File.createNewFileImpl(Native Method)
12-16 12:27:46.438: W/System.err(5797): at java.io.File.createNewFile(File.java:1257)
12-16 12:27:46.442: W/System.err(5797): at kr.ac.cbnu.incping.tcp_cloud.DownActivity.connect(DownActivity.java:88)
12-16 12:27:46.449: W/System.err(5797): at kr.ac.cbnu.incping.tcp_cloud.DownActivity$1.onClick(DownActivity.java:161)
12-16 12:27:46.453: W/System.err(5797): at android.view.View.performClick(View.java:2538)
12-16 12:27:46.453: W/System.err(5797): at android.view.View$PerformClick.run(View.java:9152)
12-16 12:27:46.453: W/System.err(5797): at android.os.Handler.handleCallback(Handler.java:587)
12-16 12:27:46.465: W/System.err(5797): at android.os.Handler.dispatchMessage(Handler.java:92)
12-16 12:27:46.465: W/System.err(5797): at android.os.Looper.loop(Looper.java:130)
12-16 12:27:46.465: W/System.err(5797): at android.app.ActivityThread.main(ActivityThread.java:3687)
12-16 12:27:46.465: W/System.err(5797): at java.lang.reflect.Method.invokeNative(Native Method)
12-16 12:27:46.465: W/System.err(5797): at java.lang.reflect.Method.invoke(Method.java:507)
12-16 12:27:46.465: W/System.err(5797): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
12-16 12:27:46.465: W/System.err(5797): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
12-16 12:27:46.465: W/System.err(5797): at dalvik.system.NativeStart.main(Native Method)
12-16 12:27:46.465: W/System.err(5797): java.io.FileNotFoundException: /mnt/sdcard/tcp_cloud/jfjebguf/000_IMG_0593.JPG????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
12-16 12:27:46.465: W/System.err(5797): (Invalid argument)
12-16 12:27:46.465: W/System.err(5797): at org.apache.harmony.luni.platform.OSFileSystem.open(Native Method)
12-16 12:27:46.465: W/System.err(5797): at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:232)
12-16 12:27:46.465: W/System.err(5797): at java.io.FileOutputStream.<init>(FileOutputStream.java:94)
12-16 12:27:46.469: W/System.err(5797): at java.io.FileOutputStream.<init>(FileOutputStream.java:66)
12-16 12:27:46.469: W/System.err(5797): at kr.ac.cbnu.incping.tcp_cloud.DownActivity.connect(DownActivity.java:98)
12-16 12:27:46.469: W/System.err(5797): at kr.ac.cbnu.incping.tcp_cloud.DownActivity$1.onClick(DownActivity.java:161)
12-16 12:27:46.469: W/System.err(5797): at android.view.View.performClick(View.java:2538)
12-16 12:27:46.469: W/System.err(5797): at android.view.View$PerformClick.run(View.java:9152)
12-16 12:27:46.469: W/System.err(5797): at android.os.Handler.handleCallback(Handler.java:587)
12-16 12:27:46.469: W/System.err(5797): at android.os.Handler.dispatchMessage(Handler.java:92)
12-16 12:27:46.469: W/System.err(5797): at android.os.Looper.loop(Looper.java:130)
12-16 12:27:46.469: W/System.err(5797): at android.app.ActivityThread.main(ActivityThread.java:3687)
12-16 12:27:46.469: W/System.err(5797): at java.lang.reflect.Method.invokeNative(Native Method)
12-16 12:27:46.469: W/System.err(5797): at java.lang.reflect.Method.invoke(Method.java:507)
12-16 12:27:46.469: W/System.err(5797): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
12-16 12:27:46.469: W/System.err(5797): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
12-16 12:27:46.469: W/System.err(5797): at dalvik.system.NativeStart.main(Native Method)
其實,我覺得我的代碼沒有語法錯誤...但它不工作。
我想問題是創建文件的失敗。
你能幫我嗎?
你有添加作家外部permition? – CoronaPintu
您的意思是這個代碼?在AndroidManifest.xml中,你必須給Permision <使用權限android:name =「android.permission.WRITE_EXTERNAL_STORAGE」/><使用權限android:name =「android.permission.WRITE_EXTERNAL_STORAGE」>使用權限> – user3079724
<使用權限android:name =「android.permission.MOUNT_UNMOUNT_FILESYSTEMS」/> – CoronaPintu