How to unzip password protected zip files in android studio? - java

I tried unzipping a zip file with password But it goes to a dead end. Like It says No read permission. I requested the permission from the code and allowed in app. Still error coming
Error - net.lingala.zip4j.exception.ZipException: no read access for the input zip file
This is the code
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.ZipFile;
public class MainActivity2 extends AppCompatActivity {
;
private FileChooserFragment fragment;
private Button buttonShowInfo;
Context context;
String vv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.buttonShowInfo = this.findViewById(R.id.button_showInfo);
this.buttonShowInfo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
showInfo();
} catch (ZipException e) {
e.printStackTrace();
}
}
});
}
private void showInfo() throws ZipException {
try {
//
System.out.println(Environment.getExternalStorageDirectory().toString());
ZipFile zipFile = new ZipFile("/storage/emulated/0/Download/1.zip);
if (zipFile.isEncrypted()) {
zipFile.setPassword("aa".toCharArray());
}
zipFile.extractAll(Environment.getExternalStorageDirectory().toString());
break;
} catch (ZipException e) {
System.out.println("nope");
e.printStackTrace();
}
}}
Password is aa
Please give me a code which works in new version of android studio and android 11 atleast

Related

Oauth2 doesnt open an browser and a token in an android app

Trying to use the Oauth authorization flow in an andoid app using java. No browser opened needed for authorizing. I don't know if i should ask here or on the api's website
permission for internet is granted in manifest.xml
MainActivity.java:
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button RankConfirm = findViewById(R.id.UsernameConfirm);
RankConfirm.setOnClickListener(v -> {
final EditText username_box = findViewById(R.id.OsuUsername);
final String Username = username_box.getText().toString();
final TextView RankView = findViewById(R.id.RankView);
API_Response.UpdateRank(Username,RankView);
});}
}
API.java
import android.util.Log;
import android.widget.TextView;
import java.io.IOException;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class API {
public static void UpdateRank(String Username, TextView RankView) {
Thread NetworkThread = new Thread ("NetworkThread"){
private volatile String Rank = "nic";
public void run() {
OkHttpClient client1 = new OkHttpClient();
Request request1 = new Request.Builder()
.url("https://osu.ppy.sh/oauth/authorize/?client_id=14403&redirect_uri=https://google.com&response_type=code&scope=public")
.build();
try {
Response response1 = client1.newCall(request1).execute();
String Code = response1.body().string();
Log.e("Debug", Code);
} catch (IOException e) {
Log.e("Debug", "IO exception1");
}```
Realized i was doing it completly wrong and i should have used a WebView

Where is this LoginActivity retrieving phoneno. and password from?

The LoginActivity.java is the first thing that gets run after I open the android app and a login screen is seen asking for phonenumber and password which it retrieves from a database. I have a mysql database related to this project running in my computer but how do I find exactly from where in the database the app is retrieving the phoneno. and password from?
LoginActivity.java
package com.example.ankit.mrestro.Controller;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import com.baidu.android.pushservice.PushConstants;
import com.baidu.android.pushservice.PushManager;
import com.squareup.otto.Subscribe;
import com.example.ankit.mrestro.Bus.BusProvider;
import com.example.ankit.mrestro.Bus.LoginEvent;
import com.example.ankit.mrestro.Bus.LoginSuccessEvent;
import com.example.ankit.mrestro.Bus.PushRegisterEvent;
import com.example.ankit.mrestro.R;
import com.example.ankit.mrestro.model.LoginResult;
import com.example.ankit.mrestro.services.DataService;
import com.example.ankit.mrestro.services.RESTrepository;
public class LoginActivity extends Activity {
public static final String PREF_ACCOUNT_ID = "cust_id";
public static final String PREF_TOKEN = "accessToken";
public static final String SHARED_PREF_DB_NAME = "loginResult";
private ProgressDialog progressDialog;
public static Intent createIntent(Context c) {
return new Intent(c, LoginActivity.class);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DataService.init();
progressDialog = new ProgressDialog(this);
/**
* Check either we are already logged in
*/
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREF_DB_NAME, 0);
if (sharedPreferences.getString(PREF_TOKEN, "").length() != 0) {
RESTrepository.setToken(sharedPreferences.getString(PREF_TOKEN, ""));
RESTrepository.setUser_id(sharedPreferences.getInt(PREF_ACCOUNT_ID, 0));
goToMainActivity();
}
setContentView(R.layout.activity_login);
PushManager.startWork(getApplicationContext(), PushConstants.LOGIN_TYPE_API_KEY,
"hwfeocSIPlgKTasIuARPREnS");
//SharedPreferences preferences=getSharedPreferences("pushService",0);
//String userId=preferences.getString("user_id","no data");
//Toast.makeText(this,"user id is:"+userId,Toast.LENGTH_SHORT).show();
Button loginButton=(Button)findViewById(R.id.email_sign_in_button);
loginButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
String phoneno=((TextView)findViewById(R.id.email)).getText().toString();
String password=((TextView)findViewById(R.id.password)).getText().toString();
// Toast.makeText(getBaseContext(),"login..."+phoneno+"..."+password,Toast.LENGTH_SHORT).show();
progressDialog.show();
BusProvider.get().post(new LoginEvent(phoneno,password));
}
});
}
#Override
protected void onResume(){
super.onResume();
BusProvider.get().register(this);
}
#Override
protected void onPause(){
super.onPause();
BusProvider.get().unregister(this);
}
#Subscribe
public void onLoginSuccessEvent(LoginSuccessEvent loginSuccessEvent){
progressDialog.hide();
LoginResult result=loginSuccessEvent.getResult();
if (result != null) {
// Toast.makeText(this,result.getCust_id()+result.getCust_name()+result.getCust_access_token(),Toast.LENGTH_SHORT).show();
//Toast.makeText(this,"Login Success",Toast.LENGTH_SHORT).show();
SharedPreferences preferences = this.getSharedPreferences(SHARED_PREF_DB_NAME, MODE_PRIVATE);
preferences.edit().putString(PREF_TOKEN,result.getCust_access_token()).commit();
preferences.edit().putInt(PREF_ACCOUNT_ID,result.getCust_id()).commit();
SharedPreferences pushPreferences=this.getSharedPreferences("pushService",0);
BusProvider.get().post(new PushRegisterEvent
(result.getCust_id(),result.getCust_access_token(),pushPreferences.getString("user_id","")));
goToMainActivity();
} else {
Toast.makeText(this, "Unable to login, please retry", Toast.LENGTH_SHORT).show();
}
}
private void goToMainActivity() {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
}
}
In Login Activity your on button click your are getting the the phone number and password of your sending it to the server and onLoginSuccessEvent you are getting your response and save it to the sharePreferences

i have made a simple music player app but it does not play music from sdcard it is working when i use it for playing a song from res/raw

This is my code for the MainActivity. I am using only one activity:
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.io.IOException;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button bPlay,bStop;
public static MediaPlayer mplayer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bPlay=(Button)findViewById(R.id.button);
bStop=(Button)findViewById(R.id.button2);
bPlay.setOnClickListener(this);
bStop.setOnClickListener(this);
}
#Override
public void onClick(View v){
switch(v.getId()){
case R.id.button:
String path = "/sdcard/honey.mp3";
//bStop.setText(path);
// Uri myUri = Uri.parse("file:///storage/emulated/0/honey.mp3/");
//Uri myUri = Uri.parse("android.resource://com.example.shubhamchauhan.mediaplayer/"+R.raw.honey);
mplayer = new MediaPlayer();
mplayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try{
mplayer.setDataSource(path);
}catch(IllegalArgumentException e){
Toast.makeText(getApplicationContext(),"You set wrong uri",Toast.LENGTH_SHORT).show();
}catch(SecurityException e){
Toast.makeText(getApplicationContext(),"You set wrong uri",Toast.LENGTH_SHORT).show();
}catch(IllegalStateException e){
Toast.makeText(getApplicationContext(),"You set wrong uri",Toast.LENGTH_SHORT).show();
}catch(IOException e){
Toast.makeText(getApplicationContext(),"You set wrong uri start",Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
try {
mplayer.prepare();
}catch(IllegalStateException e){
Toast.makeText(getApplicationContext(),"You set wrong uri",Toast.LENGTH_SHORT).show();
}catch(IOException e){
Toast.makeText(getApplicationContext(),"You set wrong uri prepare",Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
mplayer.start();
break;
case R.id.button2:
if(mplayer!=null && mplayer.isPlaying()){
mplayer.stop();
}
break;
}
}
}
How to fix it?

android- Unable to get text from textview [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
i need to read the text from textview1 but the text is always not the same as "-shutdown"
but on the textview is displayed the text "-shutdown"
Or is there any other ways to read directly text from bo.toString() withut write it in a textview??
Thanks.
My activity class:
package com.example.androrem;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.NetworkOnMainThreadException;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class ReadSMS extends Activity {
private static final int duration = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_read_sms);
doButton1();
}
private void readData(){
new Thread() {
#Override
public void run() {
String path ="http://androidremoter.altervista.org/zero/test.txt";
URL u = null;
try {
u = new URL(path);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.connect();
InputStream in = c.getInputStream();
final ByteArrayOutputStream bo = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
in.read(buffer); // Read from Buffer.
bo.write(buffer); // Write Into Buffer.
runOnUiThread(new Runnable() {
#Override
public void run() {
TextView text = (TextView) findViewById(R.id.textView1);
text.setText(bo.toString()); //the textview display "-shutdown"
String input = text.getText().toString();//get text from textview
String com1 = "-shutdown";
if(input==com1)//always show not work
{
TextView text2 = (TextView) findViewById(R.id.textView2);
text2.setText("work");
}
else
{
TextView text2 = (TextView) findViewById(R.id.textView2);
text2.setText("not work");
}
try {
bo.close();
} catch (IOException e) {
e.printStackTrace();
}
}
});
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
private void doButton1()
{
Button gettext = (Button) findViewById(R.id.test1);
gettext.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Do something in response to button click
readData();
}
});
}
You can't compare two Strings like that. You are comparing the objects rather than the contents. Do this instead:
if (input.trim().equals(com1)) {
...

Android - Threads not executing

So yeah, my thread isn't executing, or doing the code within it. I just wanted to run a shell script from my sdcard, and show a "loading" circles or "progress" circle or whatever you want to call it. When I click the button to run the thread, I get the progress/loading bar/circle, but it just sits there and does nothing. I've looked at some examples but still cannot figure out what I did wrong. Here's my code:
package com.cydeon.plasmamodz;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.TimeoutException;
import com.stericson.RootTools.*;
import com.stericson.RootTools.exceptions.RootDeniedException;
import com.stericson.RootTools.execution.CommandCapture;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Install extends Activity{
private static ProgressDialog progressDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.install);
Button bInstall = (Button) findViewById(R.id.bInstallTheme);
bInstall.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
showDialog();
}
});
}
public void showDialog(){
progressDialog = ProgressDialog.show(Install.this, "", "Installing Theme", true);
Thread thread = new Thread();
thread.start();
}
public void run(){
try{
Thread.sleep(1000);
CommandCapture command = new CommandCapture(0, "su", "sh /sdcard/plasma/scripts/install.sh");
try {
RootTools.getShell(true).add(command).waitForFinish();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
} catch (RootDeniedException e) {
e.printStackTrace();
}
}catch (InterruptedException e){
e.printStackTrace();
}
handler.sendEmptyMessage(0);
}
private static Handler handler = new Handler() {
#Override
public void handleMessage(Message msg) {
progressDialog.dismiss();
}
};
}
So, am I doing something wrong? Why won't it run my code? Thanks!
The thread doesn't know what to run. Change
public class Install extends Activity{
to
public class Install extends Activity implements Runnable {
and change
Thread thread = new Thread();
to
Thread thread = new Thread(this);

Categories

Resources