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

时间:2017-01-25 12:02:29

标签: android web upload ftp imageview

嘿,我有一个代码,让用户从图库中选择一个图像,然后他选择图像显示在图像视图中,现在当用户点击一个按钮时,它应该将图像上传到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 String是转换为字符串的图像。 任何人都可以帮助我并告诉我为什么它找不到文件?

1 个答案:

答案 0 :(得分:0)

要求用户选择图像并将该图像用于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();
            }
        }
    }

希望你也同意