【问题标题】:ftp file download result in black screenftp文件下载导致黑屏
【发布时间】:2013-08-15 23:18:07
【问题描述】:

我正在从 ftp 服务器下载一个文件。下载代码工作正常,但是下载代码屏幕没有显示任何内容后,它变黑了。下载功能也没有返回真值,即使文件被保存在指定的目录中。

  public class FTPClass{
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_file_player);

                StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);

    Intent intent = getIntent();
    dirname = intent.getStringExtra("currentDirName");


    MyFTPClient mftp = new MyFTPClient();
    createPath = mftp.getAppRootPath().concat("/"+ dirname);
    mftp.setCurrentDir(createPath);
    System.out.println(mftp.ftpChangeDirectory(createPath));

    FTPFile[] farr = mftp.ftpListAllFiles();
    System.out.println(farr.length);

    for(int i = 0; i<farr.length;i++){
        System.out.println("SRC: "+createPath+"/"+farr[i].getName());

        String src = createPath+"/"+farr[i].getName();
        System.out.println("DEST: "+"/data/data/com.example.ftpplayer" + "/app_"+dirname);
        String dest ="/data/data/com.example.ftpplayer" + "/app_"+dirname+"/"+farr[i].getName();
        System.out.println(mftp.downloadFile(src,dest));
    }
}
}

public class CallingIntent extends Activity{
        System.out.println("In item click ");
            Intent intent = new Intent(getApplicationContext(), FTPClass.class);
            String dir = ((TextView) view).getText().toString();
            intent.putExtra("currentDirName", dir);
            startActivity(intent);

}
 public class MyFTPClient{

 public boolean downloadFile(String srcPath , String destPath){

    try {
        FileOutputStream fos = new FileOutputStream(destPath);
        System.out.println(mftp.retrieveFile(srcPath, fos)); // retrieve file doesn't return true
        fos.flush();
        fos.close();
        return true;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }catch(IOException e){
        e.printStackTrace();
    }
    return false;
}

}

【问题讨论】:

  • FTPClass 应该有 extends Activity ,并且 CallingIntent 应该有一个 onCreate 方法。我没有尝试过,但我认为这不应该按原样运行。
  • 是的,我很抱歉。 FTPClass 和 CallingIntent 是一个活动。我转义了该代码。

标签: android ftp download screen


【解决方案1】:

您需要在后台线程上运行您的代码,尝试使用 asyncTask。

【讨论】:

  • Is Strictmode 策略是没有从retrieveFile 方法获得真实的原因。日志也没有显示任何错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-08
  • 2012-03-20
  • 2023-03-19
  • 1970-01-01
相关资源
最近更新 更多