Android无法找到文件/目录

时间:2018-05-28 05:33:36

标签: android filenotfoundexception

我正在尝试在Java(PC)和Android之间进行Socket程序。 APP从图库中选择图像并显示我选择的图像。当我运行程序时,我发生了这个错误。我检查了关于FİleNotFOundException的其他帖子,但我找不到任何解决方案。任何人都可以帮助我吗?

这是我的代码

public class SendfileActivity extends Activity   {
    /** Called when the activity is first created. */

    private static final int SELECT_PICTURE = 1;

    private String selectedImagePath;
    private String yol;
    private ImageView img;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        System.out.println("34");
        img = (ImageView) findViewById(R.id.ivPic);
        System.out.println("36");
        ((Button) findViewById(R.id.bBrowse))
                .setOnClickListener(new OnClickListener() {
                    public void onClick(View arg0) {
                        System.out.println("40");
                        Intent intent = new Intent();
                        intent.setType("image/*");
                        intent.setAction(Intent.ACTION_GET_CONTENT);
                        startActivityForResult(
                                Intent.createChooser(intent, "Select Picture"),
                                SELECT_PICTURE);
                        System.out.println("47");
                    }
                });
        ;
        System.out.println("51");
        Button send = (Button) findViewById(R.id.bSend);
        final TextView status = (TextView) findViewById(R.id.tvStatus);

        send.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                new Thread(new SocketThread()).start();
            }
        });
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if (requestCode == SELECT_PICTURE) {
                Uri selectedImageUri = data.getData();
                yol = selectedImageUri.getPath();
                selectedImagePath = getPath(selectedImageUri);

                        TextView path = (TextView) findViewById(R.id.tvPath);
                path.setText("Image Path : " + yol);
                img.setImageURI(selectedImageUri);

            }
        }
    }

    public String getPath(Uri uri) {
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        int column_index = cursor
                .getColumnIndex(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }

    class SocketThread implements Runnable {

        @Override
        public void run() {

            Socket sock;
            try {
                sock = new Socket("10.0.2.2", 8080);
                System.out.println("Connecting...");

                // sendfile
                File myFile = new File(yol);

                byte[] mybytearray = new byte[(int) myFile.length()];

                FileInputStream fis = null;
                try {
                    fis = new FileInputStream(myFile);
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                BufferedInputStream bis = new BufferedInputStream(fis);
                bis.read(mybytearray, 0, mybytearray.length);
                OutputStream os = sock.getOutputStream();
                System.out.println("Sending...");
                os.write(mybytearray, 0, mybytearray.length);
                os.flush();

                sock.close();
            } catch (UnknownHostException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

这是我的logcat。

W/System.err: java.io.FileNotFoundException: /document/image:78 (No such file or directory)
W/System.err: at java.io.FileInputStream.open(Native Method)
              at java.io.FileInputStream.<init>(FileInputStream.java:146)
              at com.example.murat.dhoproje.SendfileActivity$SocketThread.run(SendfileActivity.java:115)
              at java.lang.Thread.run(Thread.java:761)

1 个答案:

答案 0 :(得分:0)

  

java.io.FileNotFoundException:/ document / image:78

这是

获得的不可能的非现有文件系统路径
  

yol = selectedImageUri.getPath();

但是你必须完全不同并使用InputStream而不是FileInputStream。所以改变

  

FileInputStream fis = null;                   尝试{                       fis = new FileInputStream(myFile);

InputStream is = getContentResolver().openInputStream(data.getData());

使用is fis<?php if(isset($_GET['code'])) $acode = $_GET['code']; else die("No code!"); $con=mysqli_connect("xxx","xxx","xxx","xxx"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } else { $acode = mysqli_real_escape_string($con, $acode); $query = mysqli_query($con,"select * from login where activation_code='$acode'") or die(mysqli_error($con)); if(mysqli_num_rows($query) == 0) { echo "Wrong code"; die(); } elseif (mysqli_num_rows ($query)==1 && isset($_POST['pass'])) { $pass = mysqli_real_escape_string($con, $_POST['pass']); $query3 = mysqli_query($con,"update login set Password='$pass' where activation_code='$acode'") or die(mysqli_error($con)); echo 'Password Changed'; } } ?> enter code here <form action="resetpass.php?code=<?php echo $_GET['code'];?>" method="POST"> <p>New Password:</p><input type="password" name="pass" /> <input type="submit" name="submit" value="Signup!" /> </form> 做了什么。

顺便说一下:与socket编程没什么关系。您可能已经尝试在onActivityResult()中打开该文件,以向您和我们展示问题。