【发布时间】:2017-06-10 14:35:09
【问题描述】:
嘿,我有一个代码可以让用户从图库中选择一张图片,在他选择图片后会显示在图片视图中,现在当用户点击一个按钮时,它应该将图片上传到 ftp 服务器,但是从一些应用程序告诉我找不到我提供的文件的位置的原因。
这是 ftp 上传代码(我使用 asynctask 执行)
public UploadImage(String host,int port,String username,String password,String imagePath) {
this.host = host;
this.port = port;
this.username = username;
this.password = password;
this.imagePath = imagePath;
}
public void uploadingFilestoFtp() throws IOException
{
FTPClient con = null;
try
{
con = new FTPClient();
con.connect(host);
if (con.login(username, password))
{
con.enterLocalPassiveMode(); // important!
con.setFileType(FTP.BINARY_FILE_TYPE);
Uri uri = Uri.parse(this.imagePath);
String data = uri.getPath();
FileInputStream in = new FileInputStream(new File(data));
boolean result = con.storeFile("/img.png", in);
in.close();
if (result) Log.v("upload result", "succeeded");
con.logout();
con.disconnect();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
和加载文件到imageview
loadedImage = (ImageView)findViewById(R.id.upload_image1);
Drawable drawable = loadedImage.getDrawable();
if(drawable.getConstantState().equals(getResources().getDrawable(R.drawable.camera_icon).getConstantState())) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "SelectPicture"), SELECT_PICTURE);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
selectedImageURI = data.getData();
loadedImage = (ImageView)findViewById(R.id.upload_image1);
loadedImage.setScaleType(ImageView.ScaleType.CENTER_CROP);
Glide.with(this).load(selectedImageURI)
.into((ImageView) findViewById(R.id.upload_image1));
ImageView m =(ImageView)findViewById(R.id.remove_image);
m.setFocusable(true);
m.setVisibility(View.VISIBLE);
}
}
}
imagePath 字符串是转换为字符串的图像 uri。 谁能帮助我并告诉我为什么找不到文件?
【问题讨论】:
-
那么哪个文件没有找到?请提供确切的完整路径。或完整的uri。
The imagePath String is the image uri converted to string.。向我们展示价值! -
如果你让用户选择一个文件,你不必先把它放在一个 imageview 中。您可以直接上传文件。
-
UploadImage(String host,int port,String username,String password,String imagePath)。当你调用这个函数时,告诉我们参数imagePath的值。 -
new File(data))。您将其用作参数值。你最好像File file = new File(data));一样把它拿出来。然后用if (!file.existst()) return null;检查文件是否真的存在。但更好的是:在调用异步任务之前检查文件是否存在
标签: android web upload ftp imageview