How to delete a file from GitHub Repository programmatically from your Android App - Java code?

Published: 04 April 2024
on channel: Programmer World
51
2

This video shows the java code to delete a file from the GitHub repository. Please refer to the below for steps to committing the file in the GitHub:
https://programmerworld.co/android/ho...

I hope you like this video. For any questions, suggestions or appreciation please contact us at: https://programmerworld.co/contact/ or email at: [email protected]

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.deleteafilefrogithub;

import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.widget.TextView;

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

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

import java.io.IOException;

public class MainActivity extends AppCompatActivity {
private TextView textView;
private String stringToken = "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";

@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);
textView = findViewById(R.id.textView);

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

return insets;
});
}

public void buttonDelete(View view){
try {
GitHub gitHub = new GitHubBuilder().withOAuthToken(stringToken).build();
GHRepository ghRepository = gitHub.getRepository("programmerworld1990/CommitFileAndroidAppDemo");
GHContent ghContent = ghRepository.getFileContent("image_Github.jpg");
GHContentUpdateResponse ghContentUpdateResponse = ghContent.delete("Deleting File Demo");
textView.setText(ghContentUpdateResponse.toString());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

--