How to commit a file to GitHub programmatically from your Android app java code?

Veröffentlicht am: 21 März 2024
auf dem Kanal: Programmer World
67
5

This video shows the code to commit a file to a GitHub repository programmatically from your Android App. It uses the GitHub Java APIs from the below Maven - https://mvnrepository.com/artifact/or...

I hope you like this video. For any questions, suggestions or appreciation please contact us at: https://programmerworld.co/contact/ or email at: programmerworld1990@gmail.com

Complete source code and other details/ steps of this video are posted in the below link:
https://programmerworld.co/android/ho...

However, the main Java code is copied below also for reference:

package com.programmerworld.githubfilecommit;

import static android.Manifest.permission.READ_MEDIA_IMAGES;

import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.storage.StorageManager;
import android.os.storage.StorageVolume;
import android.view.View;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import org.kohsuke.github.GHContentBuilder;
import org.kohsuke.github.GHContentUpdateResponse;
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GitHub;
import org.kohsuke.github.GitHubBuilder;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

public class MainActivity extends AppCompatActivity {
private TextView textView;
private String stringToken = "ghp_wWZEbSftTbAvnmSP3sgr1p9fon0ay51aYEwi";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) - {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);

ActivityCompat.requestPermissions(this, new String[]{READ_MEDIA_IMAGES},
PackageManager.PERMISSION_GRANTED);

textView = findViewById(R.id.textView);
return insets;
});
}


public void buttonGithubCommit(View view){

StorageManager storageManager = (StorageManager) getSystemService(STORAGE_SERVICE);
StorageVolume storageVolume = storageManager.getStorageVolumes().get(0); // 0 for internal storage
File file = new File(storageVolume.getDirectory().getPath() + "/Download/images.jpg");

Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
GitHub gitHub = new GitHubBuilder().withOAuthToken(stringToken).build();
GHRepository ghRepository = gitHub.getRepository("programmerworld1990/CommitFileAndroidAppDemo");
GHContentBuilder ghContentBuilder = ghRepository.createContent();
byte[] bytes = Files.readAllBytes(file.toPath());
ghContentBuilder.content(bytes);
ghContentBuilder.branch("main");
ghContentBuilder.message("1st commit from Android App Demo");
ghContentBuilder.path("image_Github2.jpg");

GHContentUpdateResponse ghContentUpdateResponse = ghContentBuilder.commit();

} catch (IOException e) {
throw new RuntimeException(e);
}
}
});
thread.start();
while (thread.isAlive()){
textView.setText("Thread is processing the request");
}
textView.setText("SUCCESS");
}
}


--


Auf dieser Seite können Sie das Online-Video How to commit a file to GitHub programmatically from your Android app java code? mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer Programmer World 21 März 2024 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 67 Mal angesehen und es wurde von 5 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!