I am trying to upload an image selected from the gallery to my ftp server.
My code:
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;
}
}
When I run this code, I get:
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)
So, I know my FTP connection works as I have another class which uploads text file to my ftp correctly, but now I am trying to upload an image from the gallery and I am getting an error that there is no such file or directory for the image, so I am assuming the path is wrong or something? I am unsure on how I am doing it wrong and require some advice.
A Uri
is not a file.
If the scheme of the Uri
is file
, then it represents a file on the filesystem that, in theory, your app should be able to access. Use getPath()
to get the filesystem path.
If the scheme is anything else, it does not necessarily represent a file on the filesystem that your app can access. For example, if the scheme is http
or https
, the Uri
represents something that would be downloaded from a Web server.
If the scheme is content
, then it is backed by a ContentProvider
. Use a ContentResolver
and openInputStream()
to get an InputStream
on the content identified by the Uri
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.