我不知道为什么文件没有

时间:2013-12-16 04:57:18

标签: android makefile

请有人帮助我!

我制作了一些代码,代码是TCP Sock_program。

并且,此代码不起作用!

我认为发生此问题是因为文件未创建。

这是我的代码。 (我的手机是android 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)

实际上,我认为我的代码没有语法错误......但它不起作用。

我认为问题是创建文件失败。

你可以帮帮我吗?

1 个答案:

答案 0 :(得分:0)

你的usedID在这里有问题

String dir = file.getAbsolutePath() + String.format("/tcp_cloud/%s",userID);

因为在错误日志中你得到了。请检查..

java.io.FileNotFoundException: /mnt/sdcard/**tcp_cloud/jfjebguf/000_IMG_0593.JPG??????????????????????**
12-16 12:27:46.465: W/System.err(5797):  (Invalid argument)