在real device和file.list中请求权限无法工作

xmakbtuz  于 2021-07-11  发布在  Java
关注(0)|答案(0)|浏览(217)

我有两个android应用程序的问题。首先,我创建了python服务器和android java客户端。主要目的是将一些命令从python发送到java。所以,我的问题是here:-
1-我打电话的时候 directory.list() 并不是所有的文件都像图片一样,我的意思是图片不能出现在我面前,但其他文件通常出现在模拟器和真实设备上。
2-当我在emulator上运行代码时,每件事都很好,比如从python和java发送和接收数据,java询问我权限,然后等待我接受或dinay,但是当我在真实设备上运行相同的代码时,所有工作都很好,而不是这一行:-

else if(command.split(" ")[0].trim().equals("download"))

程序崩溃了,说“droidapp停止了”。我不知道用户请求权限中是否有错误,因为请求屏幕在emulator上出现的真实设备上没有出现,并且我无法将真实设备连接到笔记本电脑,因为我的硬件usb有问题。我很抱歉我的糟糕的方式来传递我的问题,但这就是我如何形容它。
这是我的完整javacode:-

package droid.client;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.widget.Toast;

import java.net.InetAddress;
import java.net.Socket;
import java.io.*;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.Base64;

public class MainActivity extends Activity {

    private int STORAGE_PERMISSION_CODE = 1;
    private Socket socket;
    boolean sendData = true;   // Flag to indicate whether data should be sent
    //String Str = "HelloServer";

    private static final int SERVERPORT = 2307;
    private static final String SERVER_IP = "192.168.1.8";
    private static boolean GET_FILE_PERMISSION = true;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        new Thread(new ClientThread()).start();
    }

    class ClientThread implements Runnable {
        //String path = "/sdcard";
        String path = Environment.getExternalStorageDirectory().toString() + "/Download";
        File directory = new File(path);

        @RequiresApi(api = Build.VERSION_CODES.O)
        @Override
        public void run() {
            try {
                InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
                socket = new Socket(serverAddr, SERVERPORT);
                DataInputStream Input = new DataInputStream(socket.getInputStream());
                //DataOutputStream Output = new DataOutputStream(socket.getOutputStream());
                FileInputStream getFileStream = null;
                int bufLength = 4096;
                byte[] buffer = new byte[4096];
                byte[] data;
                String ack;

                while (socket.isConnected()){
                    String command = Input.readLine();
                    String Str = "";
                    if(command.equals("ls"))
                    {
                        directory = new File(path);
                        String[] files = directory.list();
                        for (int i = 0; i < files.length; i++)
                        {
                            Str += (files[i] + "\n");
                        }
                    }
                    else if(command.split(" ")[0].equals("cd"))
                    {
                        String subPath = command.split(" ")[1];
                        if(subPath.equals(".."))
                        {
                            String[] filePath = path.split("/");
                            if(filePath.length > 1)
                            {
                                for(int i = 0; i < filePath.length - 1; i++)
                                {
                                    if(i == filePath.length - 2)
                                        Str += filePath[i];
                                    else
                                        Str += filePath[i] + '/';
                                }
                                path = Str;
                                Str = "[+] Successful, new path is : " + path;
                            }
                            else
                            {
                                Str = "[-] Error, no back available.";
                            }
                        }
                        else
                        {
                            directory = new File(path);
                            String[] files = directory.list();
                            for (int i = 0; i < files.length; i++) {
                                if (files[i].equals(subPath)) {
                                    path += '/' + subPath;
                                    Str = "[+] Successful, new path is : " + path;
                                    break;
                                }
                                if(i + 1 == files.length)
                                {
                                    Str = "[-] Error, no such file or directory.";
                                    break;
                                }
                            }
                        }
                    }
                    else if(command.split(" ")[0].trim().equals("download"))
                    {
                        String fileName = command.split(" ")[1].trim();

                        if (ContextCompat.checkSelfPermission(MainActivity.this,
                                Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
                            Toast.makeText(MainActivity.this, "You have already granted this permission!",
                                    Toast.LENGTH_SHORT).show();
                        } else {
                            requestStoragePermission();
                        }

                        getFileStream = new FileInputStream(path + "/" + fileName);

                        ByteArrayOutputStream out = new ByteArrayOutputStream();
                        int readLength;
                        while ((readLength = getFileStream.read(buffer, 0, bufLength)) != -1) {
                            out.write(buffer, 0, readLength);
                        }
                        data = out.toByteArray();
                        String imageString = Base64.getEncoder().encodeToString(data);

                        int firstIndex = 0;
                        int lastIndex = 4096;
                        int lengthOfFile = imageString.length();
                        while(lengthOfFile > 0)
                        {
                            if(lengthOfFile >= 4096)
                            {
                                sendMessage(imageString.substring(firstIndex,lastIndex));
                                firstIndex += 4096;
                                lastIndex += 4096;
                                lengthOfFile -= 4096;
                            }
                            else
                            {
                                lastIndex = (lastIndex - 4096) + lengthOfFile;
                                lengthOfFile = 0;
                                sendMessage(imageString.substring(firstIndex,lastIndex));
                            }
                            ack = Input.readLine();
                        }

                        Str = "DONE";
                        //out.close();
                        getFileStream.close();
                    }
                        sendMessage(Str);
                }
            } catch (UnknownHostException e1) {
                e1.printStackTrace();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }

        private void sendMessage(String Str){
            try {
                PrintWriter out = new PrintWriter(socket.getOutputStream());
                out.println(Str);
                out.flush();
            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    private void requestStoragePermission() {
        while(GET_FILE_PERMISSION) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_EXTERNAL_STORAGE))
            {
                new AlertDialog.Builder(this).setTitle("Permission needed").setMessage("This permission is needed because of this and that").setPositiveButton("ok", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION_CODE);
                            }
                        })
                        .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        })
                        .create().show();
            } else {
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION_CODE);
                //GET_FILE_PERMISSION = false;
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == STORAGE_PERMISSION_CODE)  {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(this, "Permission GRANTED", Toast.LENGTH_SHORT).show();
                GET_FILE_PERMISSION = false;
            } else if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_DENIED) {
                Toast.makeText(this, "Permission DENIED", Toast.LENGTH_SHORT).show();
            }
        }
    }

}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题