简体   繁体   English

使用Android应用程序将图像从devide上传到FTP

[英]Upload image from devide to FTP using android app

Hey I have a code that let the user pick an image from the gallery and after he chooses the image is shown in an image view, now when the user click a button it should upload the image to ftp server , but from some reason the app tells me that the location of the file I am giving is not found. 嘿,我有一个代码,可以让用户从图库中选择一个图像,然后在选择图像后将其显示在图像视图中,现在,当用户单击按钮时,应将图像上载到ftp服务器,但是由于某种原因,该应用程序告诉我找不到我提供的文件的位置。

here is the ftp upload code (I execute it using asynctask) 这是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();
    }


}

and the loading file to imageview 然后将文件加载到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);
        }

    }
}

The imagePath String is the image uri converted to string. imagePath字符串是将图像uri转换为字符串。 can anyone help me and tell me why isn't it finding the file? 谁能帮助我,告诉我为什么找不到文件?

Ask user to pick the image and use that image to imageView.. the path consist of the actual image path you can upload it.. 要求用户选择图像并将该图像用于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();
            }
        }
    }

Hope you are giving permission also 希望你也允许

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM