簡體   English   中英

從圖庫上傳圖像到FTP沒有這樣的文件或目錄

[英]Upload Image from Gallery to FTP No such file or directory

我正在嘗試將從圖庫中選擇的圖像上傳到我的ftp服務器。

我的代碼:

public void imageButton() {
        chooseImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                System.out.println("GETTING HERE");
                startActivityForResult(gallery, RESULT_LOAD_IMAGE);
            }
        });
    }

@Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && data != null) {
            Toast.makeText(AddProduct.this, "Image selected", Toast.LENGTH_LONG).show();
            Uri selected = data.getData();
            new uploadFTP().execute(selected.getPath());
        }
    }

    class uploadFTP extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... strings) {
            FTPClient ftpClient = new FTPClient();
            try {

                ftpClient.connect(InetAddress.getByName("address"));
                ftpClient.login("hostname", "passowrd");
                ftpClient.enterLocalActiveMode();
                if (ftpClient.isConnected()) {
                    System.out.println("Connected to FTP");
                }
                ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
                ftpClient.setFileTransferMode(FTP.BINARY_FILE_TYPE);
                String testName = "/public_html/" + System.currentTimeMillis() + "test";
                File file = new File(Arrays.deepToString(strings));
                FileInputStream fis = new FileInputStream(file);
                ftpClient.storeFile(testName,fis);
                System.out.println("Successful");




            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    }

運行此代碼時,我得到:

I/System.out: Connected to FTP
W/System.err: java.io.FileNotFoundException: [/external/images/media/217] (No such file or directory)
        at java.io.FileInputStream.open0(Native Method)
        at java.io.FileInputStream.open(FileInputStream.java:200)
W/System.err:     at java.io.FileInputStream.<init>(FileInputStream.java:150)
        at com.example.ufaza.androidsqlitesearch.AddProduct$uploadFTP.doInBackground(AddProduct.java:147)
        at com.example.ufaza.androidsqlitesearch.AddProduct$uploadFTP.doInBackground(AddProduct.java:130)
        at android.os.AsyncTask$2.call(AsyncTask.java:345)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:257)
W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
        at java.lang.Thread.run(Thread.java:784)

因此,我知道我的FTP連接可以正常工作,因為我有另一個類可以將文本文件正確上傳到ftp,但是現在我正嘗試從圖庫中上傳圖像,但出現錯誤,提示沒有此類文件或目錄圖片,所以我假設路徑錯誤或什么? 我不確定我做錯了什么,需要一些建議。

Uri不是文件。

如果 Uri的方案是file它表示文件系統上的一個文件,從理論上講,您的應用程序應該可以訪問該文件。 使用getPath()獲取文件系統路徑。

如果方案是其他方案,則它不一定代表您的應用程序可以訪問的文件系統上的文件。 例如,如果方案是httphttps ,則Uri表示將從Web服務器下載的內容。

如果方案是content ,則由ContentProvider支持。 使用ContentResolveropenInputStream()獲得有關Uri標識的內容的InputStream

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM