0

I am not play video on google drive which m uploading from application this code will record screen nd store file as video nd then upload on google drive but m not able to play uploaded video on google drive provide some solution . My code is as Below.

String email = null;
    Pattern gmailPattern = Patterns.EMAIL_ADDRESS;
    Account[] accounts = AccountManager.get(this).getAccounts();
    for (Account account : accounts) {
        if (gmailPattern.matcher(account.name).matches()) {
            email = account.name;
        }
    }

    Toast.makeText(this, "Android Device Registered Email Address: " + email, Toast.LENGTH_LONG).show();
    mMediaProjectionManager = (MediaProjectionManager) getSystemService(MEDIA_PROJECTION_SERVICE);

    // Connect to Google Drive
    mCredential = GoogleAccountCredential.usingOAuth2(getApplicationContext(), Arrays.asList(DriveScopes.DRIVE));

    mCredential.setSelectedAccountName(email);
    mService = getDriveService(mCredential);
    b1 = (Button) findViewById(R.id.button1);
    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new LongOperation().execute();
        }
    });

}

@Override
public void onStart() {
    super.onStart();

}

@Override
protected void onDestroy() {
    super.onDestroy();
    if (mRecorder != null) {
        mRecorder.quit();
        mRecorder = null;
    }
}

private class LongOperation extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... params) {
        for (int i = 0; i < 10; i++) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                Thread.interrupted();
            }
        }
        return "Executed";
    }

    @Override
    protected void onPreExecute() {
        if (mRecorder == null) {
            Intent captureIntent = mMediaProjectionManager.createScreenCaptureIntent();
            startActivityForResult(captureIntent, REQUEST_CODE);
        }
        Log.d("result", "Starting Recording");
    }

    @Override
    protected void onProgressUpdate(Void... values) {
    }

    @Override
    protected void onPostExecute(String result) {
        if (mRecorder != null) {
            mRecorder.quit();
            mRecorder = null;
            Log.d("result", "Stopping Recording");
        }
        for (int i = 0; i < 4; i++) {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                Thread.interrupted();
            }
        }
        saveFileToDrive();
    }
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    MediaProjection mediaProjection = mMediaProjectionManager.getMediaProjection(resultCode, data);
    if (mediaProjection == null) {
        Log.e("@@", "media projection is null");
        return;
    }
    String newFolder = "/FIREBASE";
    String extSdcard = Environment.getExternalStorageDirectory().toString();
    File file = new File(extSdcard + newFolder);
    file.mkdir();
    Log.d("result", "DIR:--" + file.getPath());
    Log.d("result", "DIR:--" + file.mkdir());
    final int width = 1280;
    final int height = 720;
    final int bitrate = 6000000;
    if (file.exists()) {

        File_Path = "record-" + width + "x" + height + "-" + System.currentTimeMillis() + ".mp4";
        video_file = new File(file, File_Path);
        Log.d("result", "VideoDIR:--" + video_file.getPath());
        Log.d("Result", "Video file Name" + video_file);
        Log.d("Result", "File Path" + File_Path);


        mRecorder = new ScreenRecorder(width, height, bitrate, 1, mediaProjection, video_file.getPath());
        mRecorder.start();
        Toast.makeText(this, "Screen recorder is running...", Toast.LENGTH_SHORT).show();
        moveTaskToBack(false);

    }

}

@NonNull
private Drive getDriveService(GoogleAccountCredential credential) {
    return new Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), credential)
            .build();
}

private void saveFileToDrive() {
    Thread t = new Thread(new Runnable() {
        @Override
        public void run() {
            try {


                // File's binary content
                java.io.File fileContent = new java.io.File(video_file.getPath());
                FileContent mediaContent = new FileContent("video/mp4", fileContent);
                Log.d("Result", "File Content:-" + fileContent);

                showToast("Selected " + fileContent + "to upload");
                Log.d("Result", "File URI:-" + fileContent);
                // File's meta data.
                com.google.api.services.drive.model.File body = new com.google.api.services.drive.model.File();
                body.setTitle(fileContent.getName());
                body.setMimeType("video/mp4");

                Drive.Files f1 = mService.files();
                Drive.Files.Insert i1 = f1.insert(body, mediaContent);
                com.google.api.services.drive.model.File file = i1.execute();

                if (file != null) {

                    showToast("Uploaded: " + file.getTitle());
                    Log.d("Result", "File Titile:-" + file.getTitle());
                } else {
                    Log.d("Result", "Else Part");
                }
            } catch (UserRecoverableAuthIOException e) {
                Log.d("Result", "Exception" + e.toString());
            } catch (IOException e) {
                e.printStackTrace();
                Log.d("Result", "Exception" + e.toString());
            }
        }
    });
    t.start();
}

public void showToast(final String toast) {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(getApplicationContext(), toast, Toast.LENGTH_SHORT).show();
        }
    });
}
krunal patel
  • 25
  • 1
  • 5
  • check this link : https://stackoverflow.com/questions/27409953/solved-uploading-video-to-google-drive-programmatically-android-api – Rims Jun 01 '17 at 08:12
  • cn u please tell me what's the problem in my code??? – krunal patel Jun 01 '17 at 08:20
  • Can you add any details like error problem encountered? [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask), [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) Show the community what you have tried. – abielita Jun 04 '17 at 17:20

0 Answers0