【问题标题】:Upload image from devide to FTP using android app使用 android 应用程序将图像从设备上传到 FTP
【发布时间】: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


【解决方案1】:

要求用户选择图像并将该图像用于 imageView..path 包含您可以上传它的实际图像路径..

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
            uri = data.getData();
            if (uri != null)
            {
                path = uri.toString();
                if (path.toLowerCase().startsWith("file://"))
                {
                    // Selected file/directory path is below
                    path = (new File(URI.create(path))).getAbsolutePath();
                }
                else{
                    path =  getRealPathFromUri(getApplicationContext(),uri);
                }

            }
            try {
                bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
                profileImg.setImageBitmap(bitmap);

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



    public static String getRealPathFromUri(Context context, Uri contentUri) {
        Cursor cursor = null;
        try {
            String[] proj = { MediaStore.Images.Media.DATA };
            cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }

希望你也给予许可

【讨论】:

  • 而你的getRealPathFromUri(); 是一个过时的功能。应该不需要。如果您继续使用它,如果有问题,您应该返回 anull。现在大部分时间都会发生这种情况。
  • @greenapps:好的..我在我的应用程序中使用它并得到结果,这就是我发布代码的原因!并感谢您的更新!
  • 但我不明白您为什么将其发布为答案。 OP 想要将文件上传到 FTP 服务器。 OP 将文件放入 imageview 没有任何问题。
  • @greenapps 我也遇到了同样的问题。在图像视图中加载但上传时说找不到文件。。当我研究时,我发现有些路径以'file://'开头,有些则以其他!我不知道为什么!有人建议我这个代码!它可以按预期工作..
  • 我不知道这是否会帮助他,但因为它帮助了我,我认为他可能可以解决一些问题并让他的工作
猜你喜欢
  • 2017-06-09
  • 1970-01-01
  • 2011-09-21
  • 1970-01-01
  • 1970-01-01
  • 2014-12-07
  • 1970-01-01
  • 2011-03-12
  • 2023-03-27
相关资源
最近更新 更多