Problems with uploading video to ftp

Asked

Viewed 38 times

0

I have the following class:

public class CameraReceiver extends BroadcastReceiver {

    private Cursor cursor;
    private MetodosUtils metodosUtils = new MetodosUtils();
    private Context context;

    @Override
    public void onReceive(final Context context, Intent intent) {
        this.context = context;
        cursor = context.getContentResolver().query(intent.getData(), null, null, null, null);
        cursor.moveToFirst();
        if ((metodosUtils.IsWifiConnection(context) == true) || (metodosUtils.IsMobileConnection(context) == true)) {
            new Thread(new Runnable() {
                public void run() {
                    metodosUtils.ftpUpload("ftp.exemplo.com.br", "user", "pass", "/Dados", cursor.getString(1), cursor.getString(3))
                }
            }).start();
        }
    }
}

The ftpUpload method:

public boolean ftpUpload(String server, String username, String password, String diretorio, String filePath, String fileName) {
        boolean retorno = false;
        boolean dirExists = true;
        FTPClient mFTPClient = new FTPClient();
        FileInputStream fis = null;
        File file = new File(filePath);
        String[] fileExists;
        FTPFile[] ftpFile;
        long filesizeLocal = 0;
        long filesizeFtp = 0;

        if (file.exists()) {
            try {
                mFTPClient.setControlEncoding("UTF-8");
                mFTPClient.connect(server);
                int reply = mFTPClient.getReplyCode();
                if (!FTPReply.isPositiveCompletion(reply)) {
                    mFTPClient.disconnect();
                    retorno = false;
                }
                if (!mFTPClient.login(username, password)) {
                    mFTPClient.logout();
                }
                mFTPClient.enterLocalPassiveMode();
                mFTPClient.setControlKeepAliveTimeout(500);
                mFTPClient.login(username, password);
                mFTPClient.setFileType(FTP.BINARY_FILE_TYPE);
                mFTPClient.setFileTransferMode(FTP.BINARY_FILE_TYPE);
                mFTPClient.setBufferSize(1024000);

                String[] directories = diretorio.split("/");
                for (String dir : directories) {
                    if (!dir.isEmpty()) {
                        if (dirExists) {
                            dirExists = mFTPClient.changeWorkingDirectory(dir);
                        }
                        if (!dirExists) {
                            if (!mFTPClient.makeDirectory(dir)) {
                                Toast.makeText(context, "Não é possível criar diretório remoto '" +
                                        mFTPClient.getReplyString() + "'", Toast.LENGTH_LONG).show();
                            }
                            if (!mFTPClient.changeWorkingDirectory(dir)) {
                                Toast.makeText(context, "Não é possível acessar o diretório remoto recém-criado '" +
                                        mFTPClient.getReplyString() + "'", Toast.LENGTH_LONG).show();
                            }
                        }
                    }
                }

                mFTPClient.changeWorkingDirectory(diretorio);
                fis = new FileInputStream(filePath);
                fileExists = mFTPClient.listNames();
                filesizeLocal = file.length();
                ftpFile = mFTPClient.listFiles(fileName);
                if (ftpFile.length == 1 && ftpFile[0].isFile()) {
                    filesizeFtp = ftpFile[0].getSize();
                }
                // somente executa se o arquivo não existir ou for menor que o que vai ser enviado.
                if ((Arrays.asList(fileExists).contains(fileName) == false) ||
                        ((Arrays.asList(fileExists).contains(fileName) == true) &&
                                (filesizeLocal > filesizeFtp))) {
                    if (mFTPClient.storeFile(fileName, fis) == true) {
                        retorno = true;
                    } else {
                        mFTPClient.deleteFile(fileName);
                        retorno = false;
                    }
                } else {
                    retorno = true; // se o arquivo já existe com mesmo tamanho exclui a pendencia
                }
                fis.close();
                mFTPClient.logout();
                mFTPClient.disconnect();
            } catch (IOException e) {
                Toast.makeText(context, "Error uploading " + " - " + e.getMessage(), Toast.LENGTH_LONG).show();
            }
        } else {
            retorno = true;
        }
        return retorno;
    }

Manifest:

<uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-feature android:name="android.hardware.camera" />

        <class android:name=".CameraReceiver" />
        <receiver
            android:name=".utils.CameraReceiver"
            android:enabled="true">
            <intent-filter>
                <action android:name="android.hardware.action.NEW_PICTURE" />
                <action android:name="android.hardware.action.NEW_VIDEO" />
                <action android:name="android.hardware.action.IMAGE_CAPTURE" />
                <action android:name="com.android.camera.NEW_VIDEO" />
                <action android:name="com.android.camera.IMAGE_CAPTURE" />

                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="*/*" />
            </intent-filter>
        </receiver>

The way it is works perfectly for photos taken with the camera, the problem is in the videos, because after recorded with the camera, it is sent without the extension to FTP. And if you put the extension manually in the file, it does not open.

inserir a descrição da imagem aqui

Thank you in advance.

  • @Math... sorry to bother you, but you who have always helped me would know what could be this problem?

  • @ramaral... sorry to bother you, but you who also has always helped me would know what this problem could be?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.